Code Block struct Testing: View { @EnvironmentObject var navHelp : NavigationHelper var body: some View { NavigationView { VStack { NavigationLink(destination:testing2(), tag : "testing2", selection : $navHelp.selection) { Text("Go to two") } } .navigationBarTitle("Navigation") } } } struct testing2 : View { @EnvironmentObject var navHelp : NavigationHelper var body: some View{ VStack { NavigationLink(destination:testing3(), tag : "testing3", selection : $navHelp.selection) { Text("Go to 2n3") } } .navigationBarTitle("Second Page") } } struct testing3 : View { @EnvironmentObject var navHelp: NavigationHelper var body: some View{ VStack { Button(action: { navHelp.selection = "testing2" }, label: { Text("Go to two") }) } .navigationBarTitle("Third Page") } } class NavigationHelper: ObservableObject { @Published var selection: String? = nil }
I am trying to navigate to any page inside of my NavigationView. I made an ObservableObject and passed it in as an EnvironmentObject. I found a similar answer on stack about this.
I can get it to work to returning to the first page by setting the value to nil or the second page by setting the selection to testing2.
How do I get it to work with another layer? When I add a new tagged NavigationLink the NavigationView automatically goes back to the top of the stack.