UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Any way out of backbutton mess

Forums > SwiftUI

I have three views, view one ---> view two ---> view three ----> view one

however this leads to a new back button being added every time the view three is reached via view two...

<- back

<- back

<- back

<- back

<- back

how can i avoid this, now i can add .navigationBarBackButtonHidden(true) but this only hides back button, 15-20 attempts later the navigation crashes ...

If there is a solution please suggest , i would highly prefer to use the NavigationView, thanks

I have followed the advise of having just one NavigationView at root

First View

struct ContentView: View {
    @State private var gotoSecond = false
    var body: some View {
        NavigationView {
            VStack {
                Text("I am first view")
                Button {
                    gotoSecond = true
                } label: {
                    Text("Go to Second View")
                        .font(.largeTitle)
                }

                NavigationLink("", destination: CardView(), isActive: $gotoSecond) 
            }

        }

    }
}

SecondView

struct CardView: View {
   @State private var gotoFinalView = false
    var body: some View {
        VStack {
            Text(" I am second view")
            Button {
                gotoFinalView = true
            } label: {
                Text("Go to final view")
                    .font(.largeTitle)
            }
        }

        NavigationLink("", destination: FinalView(), isActive: $gotoFinalView)
    }
}

Final View

struct FinalView: View {
    @State private var gotofirstView = false
    var body: some View {
        VStack {
            Text(" I am final view")
            Button {
                gotofirstView = true
            } label: {
                Text("go back to first view")
                    .font(.largeTitle)
            }
        }

        NavigationLink("", destination: ContentView(), isActive: $gotofirstView)
    }
}

1      

In CardView and FinalView, put those NavigationLinks inside the VStack. You should only be returning one View from a body property.

Also, every time you go to ContentView, you will get another back button, because you are hitting the NavigationView again. Try moving everything out of your ContentView except for the NavigationView.

So, something like this:

struct ContentView: View {
    var body: some View {
        NavigationView {
            FirstView()
        }
    }
}

struct FirstView: View {
    @State private var gotoSecond = false

    var body: some View {
        VStack {
            Text("I am first view")
            Button {
                gotoSecond = true
            } label: {
                Text("Go to Second View")
                    .font(.largeTitle)
            }

            NavigationLink("", destination: CardView(), isActive: $gotoSecond) 
        }
    }
}

And then in FinalView, got to FirstView instead of ContentView.

2      

@roosterboy - thanks, that worked just great ...

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.