Swiftui 工具栏项目内的按钮不能关闭工作表

Swiftui 工具栏项目内的按钮不能关闭工作表,swiftui,ios14,xcode12,Swiftui,Ios14,Xcode12,在Xcode 12 Beta 6中,在工具栏项中的按钮操作中,取消工作表不起作用 我的图纸视图如下所示: NavigationView { Form { Section { TextField("Name", text: $name) } } .navigationTitle("New Thing") .toolbar { ToolbarItem(pla

在Xcode 12 Beta 6中,在工具栏项中的按钮操作中,取消工作表不起作用

我的图纸视图如下所示:

NavigationView {
    Form {
        Section {
            TextField("Name", text: $name)
        }
    }
    .navigationTitle("New Thing")
    .toolbar {
        ToolbarItem(placement: .cancellationAction) {
            Button(action: {
                self.presentation.wrappedValue.dismiss()
            }, label: {
                Text("Cancel")
            })
        }
        ToolbarItem(placement: .confirmationAction) {
            Button(action: {
                do {
                    // some saving logic
                    try managedObjectContext.save()
                    self.presentation.wrappedValue.dismiss()
                } catch {
                    print("didn't save due to \(error.localizedDescription)")
                }
            }, label: {
                Text("Save")
            })
        }
    }
}
编辑:下面是我如何构建工作表的

var body: some View {
        List {
            ForEach(results) { result in
                HStack {
                    NavigationLink(destination: SingleResultView(result: result)) {
                        SingleResultRowView(result: result)
                    }
                }
            }
            .onDelete(perform: deleteResult)
        }
        .navigationTitle("All Results")
        .toolbar {
            ToolbarItem(placement: .primaryAction) {
                Button(action: {
                    self.isNewResultSheetPresented.toggle()
                }, label: {
                    Image(systemName: "plus.circle.fill")
                        .resizable()
                        .frame(width: 30, height: 30, alignment: .center)
                })
                .sheet(isPresented: $isNewResultSheetPresented) {
                    NewResultView()
                    // ^ this contains the code above
                        .environment(\.managedObjectContext, self.managedObjectContext)
                }
            }
        }
    }
首次显示工作表时,立即显示控制台日志:

2020-09-13 20:52:02.333679-0700 MyApp[2710:89263] 
[Presentation] Attempt to present <_TtGC7SwiftUI22SheetHostingControllerVS_7AnyView_: 0x1027b7890> on 
<_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x10270d620> 
(from <_TtGC7SwiftUIP10$194f39bd428DestinationHostingControllerVS_7AnyView_: 0x103605930>) 
which is already presenting <_TtGC7SwiftUI22SheetHostingControllerVS_7AnyView_: 0x103606d60>.
我只能向下轻扫,才能把床单拆下来

作为参考,我回到了一个更老的提交,在那里我使用了NavigationBarItems,它工作得非常好。但据我所知,这是一种我应该使用工具栏项的情况


有人知道为什么旧的self.presentation.wrappedValue.disclose在这里不起作用,或者为什么工作表要显示两次吗?

将工作表移出工具栏,如图所示

var body: some View {
    List {
        ForEach(results) { result in
            HStack {
                NavigationLink(destination: SingleResultView(result: result)) {
                    SingleResultRowView(result: result)
                }
            }
        }
        .onDelete(perform: deleteResult)
    }
    .navigationTitle("All Results")
    .sheet(isPresented: $isNewResultSheetPresented) {     // << here !!
        NewResultView()
        // ^ this contains the code above
            .environment(\.managedObjectContext, self.managedObjectContext)
    }
    .toolbar {
        ToolbarItem(placement: .primaryAction) {
            Button(action: {
                self.isNewResultSheetPresented.toggle()
            }, label: {
                Image(systemName: "plus.circle.fill")
                    .resizable()
                    .frame(width: 30, height: 30, alignment: .center)
            })
        }
    }
}

将图纸移出工具栏,如图所示

var body: some View {
    List {
        ForEach(results) { result in
            HStack {
                NavigationLink(destination: SingleResultView(result: result)) {
                    SingleResultRowView(result: result)
                }
            }
        }
        .onDelete(perform: deleteResult)
    }
    .navigationTitle("All Results")
    .sheet(isPresented: $isNewResultSheetPresented) {     // << here !!
        NewResultView()
        // ^ this contains the code above
            .environment(\.managedObjectContext, self.managedObjectContext)
    }
    .toolbar {
        ToolbarItem(placement: .primaryAction) {
            Button(action: {
                self.isNewResultSheetPresented.toggle()
            }, label: {
                Image(systemName: "plus.circle.fill")
                    .resizable()
                    .frame(width: 30, height: 30, alignment: .center)
            })
        }
    }
}

你能给我看一下你在哪里构建的代码吗?@Asperi我添加了它,谢谢你的关注!你能给我看一下你在哪里构建的代码吗?@Asperi我添加了它,谢谢你的关注!谢谢,我希望苹果能更清楚地了解这一点,互联网上的大多数示例都会将表单附在按钮上,我想知道为什么会是这样。谢谢,我希望苹果能更清楚地了解这一点,互联网上的大多数示例都会将表单附在按钮上,我想知道为什么会是这样。