Xcode 12 测试版 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")
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我运行代码。如果我通过向下拖动手势关闭模型视图,它会起作用。如果我通过点击关闭按钮关闭第一个视图,它会在关闭第二个视图时崩溃,并抛出一个致命错误:
致命错误:SheetBridge:检测到废弃的演示文稿:文件 SwiftUI,第 0 行
看起来当点击第一个视图的关闭按钮并在任何情况下关闭第二个视图时,都$showSecond没有更改为 false。
向下拖动和手动切换 $isPresented 之间有什么区别吗?
如果我使用presentationMode.wrappedValue.dismiss()而不是 binding isPredented,它也会崩溃。
解决方法是稍微延迟显示第二张纸,以便使第一张纸完全完成。
使用 Xcode 12 / iOS 14 进行测试
Button("show") {
showFirst.toggle()
}
.sheet(isPresented: $showFirst) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { // << here !!
showSecond.toggle()
}
} content: {
FirstView(isPresented: $showFirst)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
441 次 |
| 最近记录: |