【问题标题】:Return from NavigationLink after Alert shown显示警报后从 NavigationLink 返回
【发布时间】:2020-11-08 09:40:27
【问题描述】:

我正在使用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 来显示警报。

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    或者您有/想要使用@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")))
                }
            
        }
    }
    

    【讨论】:

    • 我认为绑定方式是正确的方式,有人帮我解决了我的问题。但是您的示例运行良好,尽管我确实在控制台中收到另一个警告:TestPushPop[35996:2063035] [UIContextMenuInteraction] Attempting -[UIContextMenuInteraction dismissMenu],当不处于活动状态时。这是一个客户端错误,最常见的原因是在给定的生命周期内多次调用dismiss。 (<_uivariablegesturecontextmenuinteraction:>) - 不确定这是否完全不同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 2021-04-01
    • 2010-10-14
    • 2011-10-22
    • 2016-06-03
    相关资源
    最近更新 更多