我正在使用NavigationLink
这样“推送”新视图:
NavigationLink(destination: SomeView(shown: $isActive)), isActive: $isActive) { ... }
在SomeView
内,我现在可以将shown
设置为false
以“弹出”。这行得通。
但是,如果在SomeView
中显示警报,则该警报将不再起作用。显示警报时,我还会在控制台中收到此错误消息:
popToViewController:transition: called on <_TtGC7SwiftUI41StyleContextSplitViewNavigationControllerVS_19SidebarStyleContext_ 0x7ffc1e858c00> while an existing transition or presentation is occurring; the navigation stack will not be updated.
我在做什么错了?
这是我在SomeView
内显示警报的方式:
VStack
{
// ...
}
.alert(isPresented:$showAlert)
{
Alert(title: Text("Some text"), message: Text("More text"), dismissButton: .default(Text("OK")))
}
我通过将showAlert
设置为true来显示警报。
答案 0 :(得分:0)
还是您有/想用@Binding
来做到这一点?
还是这种在视图和选项之间导航的方法?
SwiftUI pop view
如果是这样,此代码可以正常工作而没有任何错误 在iOS 13.6上测试
struct ContentView: View {
var body: some View {
NavigationView() {
NavigationLink(destination: SomeView()) {
Text("Go to child")
}
}
}
}
struct SomeView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
@State private var showAlert: Bool = false
var body: some View {
VStack() {
Text("I'm the child and if you press me an alert is comming up")
.onTapGesture {
self.showAlert.toggle()
}
Text("Go Back")
.onTapGesture {
self.presentationMode.wrappedValue.dismiss()
}
}.alert(isPresented: $showAlert)
{
Alert(title: Text("Some text"), message: Text("More text"), dismissButton: .default(Text("OK")))
}
}
}