BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

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!

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.