NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: NavigationLink back button edit?

Forums > 100 Days of SwiftUI

I have 3 views. A, B and C.

A has a NavigationLink that opens B, B has a NavigationLink to open C and C has a NavigationLink that opens B again. Basicly using B and C you can open them all the time.

Can I somehow edit the default Back button to exit B and C no matter how many times I have opened them and just go back to A?

   

As I am not sure how you have your A, B, and C views set up. I have created navigation using enums. Which basically allows you to control where you can navigate. The main logic for navigating back is almost the same as in your previous post. We manage our navigation path and when we need to go back we just remove all our navigation paths which in turn pops all the views and navigates us to initial screen.

// We create enums for our views to keep track where we navigate
enum Screens {
    case screenA
    case screenB
    case screenC
}

// We create navigation path as observable object so that we can access it everywhere
class Navigation: ObservableObject {
    @Published var path = [Screens]()
}

struct ContentView: View {
    @StateObject var navigation = Navigation()

    var body: some View {
        NavigationStack(path: $navigation.path) {
            VStack(spacing: 30) {
                Button("Go to screen A") {
                    navigation.path.append(Screens.screenA)
                }
            }
            .navigationDestination(for: Screens.self) { screen in
                NavigationController.navigate(to: screen)
            }
        }
        .environmentObject(navigation)
    }
}

// We create navigation controller to do the navigation to our views
class NavigationController {
    @ViewBuilder
    static func navigate(to screen: Screens) -> some View {
        switch screen {
        case .screenA:
            ScreenA()
        case .screenB:
            ScreenB()
        case .screenC:
            ScreenC()
        }
    }
}

// This is our view A

struct ScreenA: View {
    @EnvironmentObject var navigation: Navigation

    var body: some View {
        VStack(spacing: 30) {
            Image(systemName: "a.square.fill").font(.largeTitle).foregroundColor(.green)
            Button("Go to screen B") {
                navigation.path.append(Screens.screenB)
            }
            .navigationDestination(for: Screens.self) { screen in
                NavigationController.navigate(to: screen)
            }
        }
        // We are hiding default back button
        .navigationBarBackButtonHidden()
        // We assign our button that pop to root view
        .toolbar {
            ToolbarItem(placement: .navigationBarLeading) {
                Button("Back") {
                    navigation.path.removeAll()
                }
            }
        }
    }
}

// This is our view B

struct ScreenB: View {
    @EnvironmentObject var navigation: Navigation

    var body: some View {
        VStack(spacing: 30) {
            Image(systemName: "b.square.fill").font(.largeTitle).foregroundColor(.red)
            Button("Go to screen C") {
                navigation.path.append(Screens.screenC)
            }
            .navigationDestination(for: Screens.self) { screen in
                NavigationController.navigate(to: screen)
            }
        }
        // We are hiding default back button
        .navigationBarBackButtonHidden()
        // We assign our button that pop to root view
        .toolbar {
            ToolbarItem(placement: .navigationBarLeading) {
                Button("Back") {
                    navigation.path.removeAll()
                }
            }
        }
    }
}

// This is our view C

struct ScreenC: View {
    @EnvironmentObject var navigation: Navigation

    var body: some View {
        VStack(spacing: 30) {
            Image(systemName: "c.square.fill").font(.largeTitle).foregroundColor(.purple)
            Button("Go to screen B") {
                navigation.path.append(Screens.screenB)
            }
            .navigationDestination(for: Screens.self) { screen in
                NavigationController.navigate(to: screen)
            }
        }
        // We are hiding default back button
        .navigationBarBackButtonHidden()
        // We assign our button that pop to root view
        .toolbar {
            ToolbarItem(placement: .navigationBarLeading) {
                Button("Back") {
                    navigation.path.removeAll()
                }
            }
        }
    }
}

   

You the best bro!

   

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

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

Reply to this topic…

You need to create an account or log in to reply.

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.