0

This is a simple code that I am trying to test for my required feature. I want to be able to click on the button or something to take me back to the FirstView without me needing to click the back button each time.

I found that if I add another NavigationLink() with destination to FirstView() in the ThirdView() then it will stack the FirstView() on top of ThirdView().

The action that I want to perform is

Current Output:

  • Navigation Stack Before clicking "Go to First View" : FirstView() -> SecondView() -> ThirdView()

  • Navigation Stack After clicking "Go to First View": FirstView() -> SecondView()

Desired Output:

  • Navigation Stack Before clicking "Go to First View" : FirstView() -> SecondView() -> ThirdView()

  • Navigation Stack After clicking "Go to First View": FirstView()

struct FirstView: View {
    var body: some View {
        NavigationView {
            VStack {
                Text("First View")
                    .font(.headline)
                    .padding()
                
                
                NavigationLink(destination: SecondView()) {
                    Text("Go to Second View")
                }
            }
        }
    }
}

struct SecondView: View {
    var body: some View {
        VStack {
            Text("Second View")
                .font(.headline)
                .padding()
            
            NavigationLink(destination: ThirdView()) {
                Text("Go to Third View")
            }
        }
    }
}

struct ThirdView: View {
    @Environment(\.dismiss) var dismiss
    
    var body: some View {
        VStack {
            Text("Third View")
                .font(.headline)
                .padding()
            
            Button(action: {
                dismiss()
            }) {
                Text("Go to First View")
            }
        }
    }
}

1 Answers1

0

In your ThirdView, just replace @Environment(.dismiss) with @Environment(.presentationMode).

It allows you to dismiss the ThirdView and stack and go back to the FirstView using presentationMode.wrappedValue.dismiss().

With this approach, you can navigate back to the FirstView without stacking views on top of each other.

struct ThirdView: View {
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        VStack {
            Text("Third View")
                .font(.headline)
                .padding()
            
            Button(action: {
                presentationMode.wrappedValue.dismiss()
            }) {
                Text("Go to First View")
            }
        }
    }
  • Unfortunately, replacing @Environment(.dismiss) with @Environment(.presentationMode) did not make any difference. I would still end up on the SecondView() – Rahul Adepu Jun 28 '23 at 16:40
  • Several of your recent answers here appear likely to have been entirely or partially written by AI (e.g., ChatGPT). Please be aware that [posting AI-generated content is not allowed here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. Thanks! – NotTheDr01ds Jul 05 '23 at 13:25
  • **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. The moderation team can use your help to identify quality issues. – NotTheDr01ds Jul 05 '23 at 13:25