9

I have this tiny sample of code in trying to use the new NavigationStack, but I get an error: A NavigationLink is presenting a value of type “TransactionRoute” but there is no matching navigation destination visible from the location of the link. The link cannot be activated.

What am I doing wrong?

    import SwiftUI

    struct TransactionRoute: Identifiable {
        let id = UUID()
        let realm: TransactionRealm
        let transaction: String?
    }

    extension TransactionRoute: Equatable, Hashable {
        static func == (lhs: TransactionRoute, rhs: TransactionRoute) -> Bool {
            lhs.id == rhs.id
        }

        public func hash(into hasher: inout Hasher) {
            hasher.combine(id)
        }
    }

    enum TransactionRealm: Int {
        case basic
        case upcoming
        case recurring
    }

    struct ContentView: View {
        @State var navigationPath = NavigationPath()

        var body: some View {
            NavigationStack(path: $navigationPath) {
                VStack {
                    Image(systemName: "globe")
                        .imageScale(.large)
                        .foregroundColor(.accentColor)
                        .padding(.bottom, 24)
                        .onTapGesture {
                            navigationPath.append(TransactionRoute(realm: .basic, transaction: nil))
                        }
                    Text("Hello, world!")
                        .onTapGesture {
                            navigationPath.append(TransactionRoute(realm: .upcoming, transaction: nil))
                        }
                }
            }
            .navigationDestination(for: TransactionRoute.self) { route in
                switch route.realm {
                case .basic:
                    Text("Basic")

                case .upcoming:
                    Text("Upcoming")

                case .recurring:
                    Text("Recurring")
                }
            }
        }
    }

1 Answer 1

21

Like most other navigation modifiers (navigationTitle, navigationBarTitleDisplayMode), navigationDestination goes inside the NavigationStack. Think of it as if the NavigationStack gets information from within its braces.

NavigationStack(path: $navigationPath) {
    VStack {
        /// ...
    }
    .navigationDestination(for: TransactionRoute.self) { route in
        switch route.realm {
        case .basic:
            Text("Basic")

        case .upcoming:
            Text("Upcoming")

        case .recurring:
            Text("Recurring")
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

And they need 3 navigationDestinations instead of a case statement
@malhal shouldn't the switch case work though? I thought you only needed a different navigationDestination when the data type is different

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.