我找不到一种方法可以在不显示第二个NavigationView
的情况下从ViewB转到ContentView。
struct ContentView: View {
var body: some View {
NavigationView{
VStack {
Text("Go to ViewA")
NavigationLink(destination: ViewA()) {Text("Go")}
}.navigationBarTitle("ContentView")
}
}
}
struct ViewA: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
VStack {
Text("Go to ViewB")
NavigationLink(destination: ViewB()) {Text("Go to B")}
Text("Go back")
Button(action: { self.presentationMode.wrappedValue.dismiss() }) {Text("Go to ContentView")}
}.navigationBarTitle("ViewA")
}
}
struct ViewB: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
VStack {
Text("Go Home")
NavigationLink(destination: ContentView()) {Text("Go")}
Text("Go back")
Button(action: { self.presentationMode.wrappedValue.dismiss() }) {Text("Go to ViewB")}
}.navigationBarTitle("ViewB")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
在“旧”的Swift中,我可以很容易地创建一个unwindSegue,例如:
@IBAction func unwindToVCMain(segue:UIStoryboardSegue) { }
}
并且可以轻松地像这样调用它
performSegue(withIdentifier: "unwindToVcMain", sender: nil)