0

I have these 3 different views they all connect to each other through a NavigationView. I am trying to find a way to navigate to a page earlier in the NavigationView but I want the transition to seem like it is going backwards.

struct Testing: View {
    
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination:testing2()) { Text("Go to two") }
            }
            .navigationBarTitle("Navigation")
        }
    }
}

struct testing2 : View {

    var body: some View{
        VStack {
            NavigationLink(destination: testing3()) { Text("Go to third") }
        }
        .navigationBarTitle("Second Page")
    }
}

struct testing3 : View {

    var body: some View{
        VStack {
            Text("Go back to first")
        }
        .navigationBarTitle("Third Page")
    }
}

I have found a solution for this using a combination of an Observable Object, an Environment variable and tags and selections on the NavigationLink.

Is their a simpler solution to making the navigation transition go to a specific page and if it is a view that is before the current view preform the backwards transition?

runemonster
  • 177
  • 1
  • 11
  • There is some good info at [this question and answer](https://stackoverflow.com/q/57334455/1630618) – vacawama Sep 28 '20 at 00:51
  • I just looked through the post and one of the newer answers did something similar to what I was saying about Environment Variable. So I think I am going to stick with my solution. – runemonster Sep 28 '20 at 01:06

2 Answers2

0

A basic workaround is to add a @Binding and pass it along. check the binding index on appear. if it's not the index presentation.wrappedValue.dismiss until it is. A more complicated way is to add a Navigator class that handles all navigation logic.

dzren12
  • 41
  • 1
  • 6
0

Much simpler solution is to modify your third view as following:

struct testing3 : View {
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    
    var body: some View{
        VStack {
            Button(action: {
                self.presentationMode.wrappedValue.dismiss()
                self.presentationMode.wrappedValue.dismiss()
            }) {
                Text("Go back to first")
            }
            
        }
        .navigationBarTitle("Third Page")
    }
}
Khashayar
  • 357
  • 3
  • 12
  • Would this allow me to navigate to any view that is inside of the `NavigationView` or just back to the top level of the `NavigationView`? – runemonster Sep 28 '20 at 13:32
  • You can go to any parent view. Depending on how many times you write ` self.presentationMode.wrappedValue.dismiss() ` – Khashayar Sep 28 '20 at 13:38
  • I like the answer but I am going to continue using the Environment variable combination. Instead of having to say dismiss 3 times when I am on the 5th page to get to the 2nd page I just have to set the environment variable to the correct tag. – runemonster Sep 28 '20 at 16:33