我想做最简单的事情。我只想以编程方式调用一个新的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