我找不到从 ViewB 转到 ContentView 而不在NavigationView
另一个顶部显示第二个的方法NavigationView
。
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()
}
}
在“旧”斯威夫特我可以很容易地做一个 unwindSegue 像
@IBAction func unwindToVCMain(segue:UIStoryboardSegue) { }
}
并轻松地将其称为
performSegue(withIdentifier: "unwindToVcMain", sender: nil)