无法执行简单的导航以查看和返回 SwiftUI 导航栏按钮 [英] Can't do a Simple Navigate to View and back SwiftUI Navigation Bar Button

查看:21
本文介绍了无法执行简单的导航以查看和返回 SwiftUI 导航栏按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一个简单的 SwiftUI 导航从一个视图到另一个视图并返回条形按钮项.我尝试了三种不同的方法来调用新视图.在 body 视图中使用 Button 有效,但在导航中使用 NavigationBarItemsbar 以两种不同的方式失败.

I'm trying to do a simple SwiftUI navigation from one view to another and back using a bar button item. I have tried three different approaches to calling a new view. Using a Button in the body view works, but using NavigationBarItems in the navigation bar fails in two different ways.

这是开始视图:

struct ContentView: View {
    @State private var showSecondView = false
    var body: some View {
        NavigationView {
            VStack {
                Text("This is the content view")
                    .navigationBarTitle("Nav Title")
//this works ONCE only:
                    .navigationBarItems(trailing: Button(action: {self.showSecondView.toggle()}) {
                        Text("SecondView")
                    })
//this always fails on return to contentview with error:
//Tried to pop to a view controller that doesn't exist
//                .navigationBarItems(trailing:
//                    NavigationLink(destination: SecondView()) {
//                        Text("SecondNav")
//                    }
//                )
//This always works:
                Button(action: {self.showSecondView.toggle()} ) {
                    Text("Call Modal Second View")
                }.padding()
                Text(self.showSecondView ? "true" : "false")
            }.sheet(isPresented: $showSecondView) {
                SecondView()
            }
        }
    }
}

如果我在 NavigationBarItems 中使用 NavigationLink,则显示 SecondView,但返回 ContentView 时,它崩溃并显示错误:试图弹出视图不存在的控制器"

If I use a NavigationLink in the NavigationBarItems, the SecondView is displayed, but on return to the ContentView, it crashes with the error: "Tried to pop to a view controller that doesn't exist"

如果我在 NavigationBarItems 中使用 Button,则过渡到 SecondView 有效一次而且只有一次.返回 ContentView 有效,但按钮不再起作用.有趣的是,如果采取的第一个动作是使用 Body 中的 Button,则NavigationBarItem 甚至一次都不起作用.

If I use a Button in the NavigationBarItems, the transition to the SecondView works once and only once. The return to ContentView works but the button no longer functions. Interestingly, If the first action taken is with the Button in the Body, the NavigationBarItem does not work even once.

还有简单的 SecondView:

And the simple SecondView:

struct SecondView: View {
    @Environment(\.presentationMode) var presentation
    var body: some View {
        NavigationView {
            VStack{
                Text("This is the second view")
                Button(action: { self.presentation.wrappedValue.dismiss()}) {
                    Text("Dismiss Modal")
                }.padding()
            }
        }
    }
}

我很困惑.任何指导将不胜感激.Xcode 11.2 (11B44), Catalina 10.15

I'm confused. Any guidance would be appreciated. Xcode 11.2 (11B44), Catalina 10.15

推荐答案

这对我来说仍然是一个问题,我在使用 popover (Modal) 演示和通过 NavigationLink 推送第二个控制器时遇到了同样的问题在 navigationBarItems 中.

This is still an issue for me, I am having the same issue with popover (Modal) presentation and pushing Second controller via NavigationLink in navigationBarItems.

这是一个非常严重的错误.它正确工作的唯一方法是 NavigationLink 位于 NavigationView 内容内而不是 navigationBarItems 内.

This is a really serious bug. The only way it works correctly is when the NavigationLink is inside NavigationView content and not navigationBarItems.

这真的很糟糕,因为 NavigationBar 按钮应该以这种方式工作.

This is a really breaking as NavigationBar buttons are suppose to work that way.

这篇关于无法执行简单的导航以查看和返回 SwiftUI 导航栏按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆