gpt4 book ai didi

SwiftUI:@Environment(\.presentationMode) 的关闭在 iOS14 中不起作用

转载 作者:行者123 更新时间:2023-12-05 08:03:28 25 4
gpt4 key购买 nike

我有一个 View 显示用于过滤列表中项目的工作表。该 View 具有此变量:

struct JobsTab: View {

@State private var jobFilter: JobFilter = JobFilter()

var filter: some View {
Button {
self.showFilter = true
} label: {
Image(systemName: "line.horizontal.3.decrease.circle")
.renderingMode(.original)
}
.sheet(isPresented: $showFilter) {
FilterView($jobFilter, categoriesViewModel, jobsViewModel)
}
}

但是,在工作表中,我正在尝试以下操作,但我无法在单击“完成”按钮时关闭 View ,只能在“取消”按钮上关闭 View :

struct FilterView: View {
@Environment(\.presentationMode) var presentationMode
@ObservedObject var categoriesViewModel: CategoriesViewModel
@ObservedObject var jobsViewModel: JobsViewModel
let filterViewModel: FilterViewModel

@Binding var jobFilter: JobFilter

@State private var draft: JobFilter
@State var searchText = ""
init(_ jobFilter: Binding<JobFilter>, _ categoriesViewModel: CategoriesViewModel, _ jobsViewModel: JobsViewModel) {
_jobFilter = jobFilter
_draft = State(wrappedValue: jobFilter.wrappedValue)
self.categoriesViewModel = categoriesViewModel
self.jobsViewModel = jobsViewModel
self.filterViewModel = FilterViewModel()
}
...
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button("FilterView.Button.Cancel.Text".capitalizedLocalization) {
presentationMode.wrappedValue.dismiss()
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button("FilterView.Button.Done.Text".capitalizedLocalization) {
let request = Job.defaultRequest()

request.predicate = filterViewModel.buildPredicate(withJobFilterDraft: self.draft)
request.sortDescriptors = [NSSortDescriptor(key: #keyPath(Job.publicationDate), ascending: false)]

jobsViewModel.filteredJobsFetchRequest = request
self.jobFilter = self.draft
presentationMode.wrappedValue.dismiss()
}
}
}

我也试过 @Binding就像保罗说的here但没有运气。是否有任何解决方法,或者我做错了什么?

提前致谢!

编辑:我已经发布了两个 View 的属性,因为我认为问题来自 FilterView 中的行self.jobFilter = self.draft .我在这里要做的是创建一个过滤器 View ,当用户按下 DONE 按钮时将执行上述行:我想分配我的绑定(bind) jobFilterJobsTab FilterView 的值真实来源(这是一个 @State )并且可能,因为我正在更新绑定(bind) jobFilter FilterView即使 $showFilter 再次显示是false ?老实说,我不知道。

EDIT2:我也试过``如果 #available(iOS 15.0, *) {让 _ = Self._printChanges()} 别的 {//回退到早期版本

in both `FilterView` and its called `JobTabs` and in both, I get the same result: unchanged

最佳答案

根据您的代码,我假设您的 FilterView() 不是 subview ,而是一个独立的 View 。因此,为确保“presentationMode.wrappedValue.dismiss()”有效,您无需在 FilerView() 外部创建@Binding 或@State 变量来在不同 View 之间来回传递数据。只需在 FilterView() 中创建一个变量即可使其工作。我没有你的完整代码,但我创建了一个与你的问题类似的情况,如下代码:

import SwiftUI

struct Main: View {
@State private var showFilter = false
var body: some View {
Button {
self.showFilter = true
} label: {
Image(systemName: "line.horizontal.3.decrease.circle")
.renderingMode(.original)
}
.sheet(isPresented: $showFilter) {
FilterView()
}
}
}

struct FilterView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {
NavigationView {
VStack {
Text("Filter View")
}.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button {
presentationMode.wrappedValue.dismiss()
} label: {
Text("cancel")
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button {
presentationMode.wrappedValue.dismiss()
} label: {
Text("okay")
}
}
}
}
}
}

关于SwiftUI:@Environment(\.presentationMode) 的关闭在 iOS14 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72885667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com