我正在尝试做最简单的事情。我只想以编程方式调用一个新的 SwiftUI 视图——不是使用按钮,而是使用代码。我已经阅读了几十篇关于此的帖子和 Apple 文档 - 但我发现的几乎所有内容都与已重命名或弃用的代码有关。我发现的最接近的是:
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