Come back to a specific View in SwiftUI (popToViewController)

久未见 提交于 2020-01-01 16:35:38

问题


Is it possible in SwiftUI to come back to a specific view? Let's say I have three views this way:

struct View1: View {
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: View2()) {
                    Text("Navigate to View2")
                }
                .navigationBarTitle("View1")
            }
        }
    }
}

struct View2: View {
    var body: some View {
        NavigationLink(destination: View3()) {
            Text("Navigate to View3")
        }
        .navigationBarTitle("View2")
    }
}

struct View3: View {
    var body: some View {
        Text("View3!")
    }
}

#if DEBUG
struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    View1()
  }
}
#endif

The navigation works back and forth:

View1->View2->View3
View3->View2->View1

Is it possible to directly come back to View1 from the View3? What I'm looking for is something similar to the UIKit

func popToViewController(_ viewController: UIViewController, 
                animated: Bool) -> [UIViewController]?

来源:https://stackoverflow.com/questions/57700532/come-back-to-a-specific-view-in-swiftui-poptoviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!