Xcode 12 beta 4
我有一个ContentView,并包含两个不同的模态视图。我想使用sheet(isPresented: onDismiss: content:)
来显示第一个视图,当它被解除时,自动显示第二个视图。
这是我的代码:
struct ContentView: View {
@State var showFirst = false
@State var showSecond = false
var body: some View {
VStack(spacing: 20) {
Text("showFirst: \(showFirst.description)")
Text("showSecond: \(showSecond.description)")
Button("show") {
showFirst.toggle()
}
.sheet(isPresented: $showFirst) {
showSecond.toggle()
} content: {
FirstView(isPresented: $showFirst)
}
Text("")
.sheet(isPresented: $showSecond) {
SecondView(isPresented: $showSecond)
}
}
}
}
struct FirstView: View {
@Binding var isPresented: Bool
var body: some View {
VStack {
Button("close") {
isPresented = false
}
Text("First View")
}
}
}
struct SecondView: View {
@Binding var isPresented: Bool
var body: some View {
VStack {
Button("close") {
isPresented = false
}
Text("Second View")
}
}
}
然后我运行了代码。
如果我通过向下滑动手势关闭模态视图,它可以正常工作。
如果我通过点击关闭按钮来关闭第一个视图,在关闭第二个视图时会崩溃并抛出致命错误:
Fatal error: SheetBridge: abandoned presentation detected: file SwiftUI, line 0
我的困惑
看起来当点击第一个视图的关闭按钮并在任何情况下关闭第二个视图时,$showSecond
没有改变为 false。
拖动和手动切换 $isPresented
是否有区别?
如果我使用 presentationMode.wrappedValue.dismiss()
而不是绑定 isPresented
,它也会崩溃。