简体   繁体   English

推送,Segue,召唤,导航以编程方式查看SwiftUI

[英]Push, Segue, Summon, Navigate to View Programmatically SwiftUI

I'm trying to do the simplest of things. 我正在尝试做最简单的事情。 I just want to summon a new SwiftUI view programmatically - not with a button, but with code. 我只想以编程方式调用新的SwiftUI视图-而不是按钮,而是代码。 I've read a couple of dozens posts and Apple docs on this - but almost all that I've found relates to code that has been renamed or deprecated. 我已经阅读了数十篇有关此问题的文章和Apple文档-但我发现的几乎所有内容都与已重命名或不推荐使用的代码有关。 The closest I have found is: 我找到的最接近的是:

NavigationLink(destination: NewView(), isActive: $something) {
    EmptyView()
}

But this does not work for me in Xcode Beta 7. Here's the trivial app: 但这在Xcode Beta 7中对我不起作用。这是简单的应用程序:

struct ContentView: View {
    @State private var show = false

    var body: some View {

        VStack {
            Text("This is the ContentView")

            Toggle(isOn: $show) {
                Text("Toggle var show")
            }
            .padding()

            Button(action: {
                self.show = !self.show
            }, label: {
                Text(self.show ? "Off" : "On")
            })
            Text(String(show))

            //this does not work - the ContentView is still shown
            NavigationLink(destination: SecondView(), isActive: $show)
            {
             EmptyView()
            }

            //this does not work - it adds SecondView to ContentView
            //I want a new view here, not an addition
            //to the ContentView()
//            if show {
//                //I want a new view here, not an addition to the ContentView()
//                SecondView()
//            }
        }
    }

}

And the brutally simple destination: 而残酷的目的地:

struct SecondView: View {
    var body: some View {
        Text("this is the second view!")
    }
}

I must be missing something extremely simple. 我一定会错过一些非常简单的东西。 Any guidance would be appreciated. 任何指导将不胜感激。 iOS 13.1, Catalina 19A546d, Xcode 11M392r iOS 13.1,Catalina 19A546d,Xcode 11M392r

A couple of things. 有几件事。 First, NavigationLink must be imbedded in a NavigationView to work. 首先,必须将NavigationLink嵌入到NavigationView中才能工作。 Second, the link doesn't need a view as you showed it. 其次,该链接不需要您显示的视图。 This should show the second view. 这应该显示第二个视图。 I will leave to you to update the other elements. 我将请您更新其他元素。

 var body: some View {
    NavigationView{
        VStack {
            Text("This is the ContentView")

            Toggle(isOn: $show) {
                Text("Toggle var show")
            }
            .padding()

            Button(action: {
                self.show = !self.show
            }, label: {
                Text(self.show ? "Off" : "On")
            })
            Text(String(show))

            //this does not work - the ContentView is still shown
            NavigationLink(destination: SecondView()){
            Text("Click to View")}
            Spacer()
//                {
//                    EmptyView()
//                }

            //this does not work - it adds SecondView to ContentView
            //I want a new view here, not an addition
            //to the ContentView()
            //            if show {
            //                //I want a new view here, not an addition to the ContentView()
            //                SecondView()
            //            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM