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

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?

2      

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()
                }
            }
        }
    }
}

2      

You the best bro!

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.