【发布时间】:2021-01-11 00:52:54
【问题描述】:
我在 ios 1 中运行以下代码。当按下“视图 A”按钮时,状态变量 nextModalView2 设置为 NextModalView2.a 但是当执行 Sheet() 调用时,nextModalView2 的值被重置为 .none .
这怎么可能?
此外,如果您查看屏幕截图,您会看到调试器控制台面板显示的 nextModalView2 值与调试器变量面板中显示的值不同。怎么样?
enum NextModalView2{
case none
case a
case b
}
struct Menu2: View {
@State private var isShowModally = false
@State private var nextModalView2 = NextModalView2.none
var body: some View {
VStack(alignment: .center) {
Button(action: {
self.isShowModally = true
self.nextModalView2 = .a
print("self.nextModalView = \(self.nextModalView2)")
}){ Text("View A")
}
Divider()
Button(action: {
self.isShowModally = true
self.nextModalView2 = .b
}){ Text("View B")
}
.padding(.top, 20)
}
.sheet(isPresented: $isShowModally){
Group{
if self.nextModalView2 == .a {
// case NextModalView2.a:
AnyView(Text("A"))
}
if self.nextModalView2 == NextModalView2.b{
AnyView(Text("B"))
}
if self.nextModalView2 == NextModalView2.none{
AnyView(EmptyView())
}
}
}
}
}
【问题讨论】:
-
当您的视图出现在屏幕上时,会配置 .sheet() 修饰符。所以在 init() 上,.sheet() 将返回 .none。解决方案是避免在工作表关闭中打开内容。我在这里为此提供了 2 个超级简单的解决方案:stackoverflow.com/questions/65416673/…
标签: swiftui