【问题标题】:Buttons inside ToolbarItem cannot dismiss sheetToolbarItem 内的按钮无法关闭工作表
【发布时间】:2020-09-14 04:05:32
【问题描述】:

在 Xcode 12 Beta 6 中,关闭工作表在 ToolbarItem 内的按钮操作中不起作用。

我的工作表视图如下:

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 的旧提交,它运行良好。但据我了解,这是我应该使用 ToolbarItem 的情况。

有谁知道为什么旧的 self.presentation.wrappedValue.dismiss() 在这里不起作用,或者为什么工作表会出现两次?

【问题讨论】:

  • 你能展示一下你在哪里构造.sheet的代码吗?
  • @Asperi 我添加了,感谢您查看!

标签: swiftui ios14 xcode12


【解决方案1】:

将工作表移出工具栏

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)
            })
        }
    }
}

【讨论】:

  • 谢谢,我希望苹果能更清楚地说明这一点,互联网上的大多数示例都附在按钮上,我想知道为什么会这样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 1970-01-01
  • 1970-01-01
  • 2019-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多