代码之家  ›  专栏  ›  技术社区  ›  JohnSF

推,Segue,召唤,导航到以编程方式快速查看

  •  0
  • JohnSF  · 技术社区  · 6 年前

    我想做最简单的事情。我只想以编程方式调用一个新的SwiftUI视图-不是用按钮,而是用代码。我已经读过几十篇关于这个的文章和苹果文档——但我发现的几乎所有内容都与被重命名或弃用的代码有关。我发现最接近的是:

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

    但在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()
    //            }
            }
        }
    
    }
    

    还有一个极其简单的目的地:

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

    我一定错过了一些非常简单的东西。任何指导都将不胜感激。 iOS 13.1,卡特琳娜19A546d,Xcode 11M392r

    1 回复  |  直到 6 年前
        1
  •  2
  •   Michael Rivers    6 年前

    有几件事。首先,NavigationLink必须嵌入到NavigationView中才能工作。第二,链接不需要你展示的视图。这应该显示第二个视图。我将留给你更新其他元素。

     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()
                //            }
            }
        }
    }