-2

I'm using the new NavigationStack from SwiftUI. I'm using it with enum route switching.

How can I bring a "value:" to the "destination" view from the NavigationLink just like I would for a regular NavigationLink without the enum Route switch?

Here is my code:

import SwiftUI

enum RouteNew {
    case linkOne, linkTwo
}

struct Nav: View {
    @State private var pathNew = NavigationPath()
    
    var body: some View {
        NavigationStack(path: $pathNew) { // Stack with Path binding
            
            NavigationLink("Link One", value: RouteNew.linkOne) // How to give the value here that I want to bring into the destinatio  view?
            NavigationLink("Link Two", value: RouteNew.linkTwo) // How to give the value here that I want to bring into the destinatio  view?
            
            .navigationDestination(for: RouteNew.self) { route in
                switch route { // switch
                    case.linkOne:
                        viewOne(path: $pathNew)
                    case.linkTwo:
                        viewTwo(path: $pathNew)
                }
            }
        }
    }
}

struct viewOne: View {
    @Binding var path: NavigationPath
    var valueToImport: String  // How to bring this value in from the Navigation link?
    var body: some View {
        Text("View One value: \(valueToImport)")
    }
}

struct viewTwo: View {
    @Binding var path: NavigationPath
    var valueToImport: String // How to bring this value in from the Navigation link?
    var body: some View {
        Text("View Two value: \(valueToImport)")
    }
}

struct viewThree: View {
    @Binding var path: NavigationPath
    var valueToImport: String // How to bring this value in from the Navigation link?
    var body: some View {
        Text("View Three value: \(valueToImport)")
    }
}
Timmy
  • 4,098
  • 2
  • 14
  • 34
Pro Girl
  • 762
  • 7
  • 21

2 Answers2

1

Keeping the global routing you can pass the value to view using case let.

eg:

enum Route: Hashable {
    case list
    case create(Animal)
    case detail(Animal)
}

then in your routing pass it like this:

@main
struct LearnNavApp: App {
    var body: some Scene {
        WindowGroup {
            NavigationStack {
                ContentView()
                    .navigationDestination(for: Route.self) { route in
                        switch route {
                            case .list:
                                Text("Show all animals")
                            case .create(let animal):
                                Text("Create an animal \(animal.name)")
                            case .detail(let animal):
                                Text("Detail \(animal.name)")
                        }
                    }
            }
                
        }
    }
}

Call it like this:

NavigationLink("Go to Detail", value: Route.detail(Animal(name: "Rabbit")))

Your can read this original article for more explanation

Salim
  • 85
  • 11
0

Forget about "routing" and use destination values as designed:

.navigationDestination(for: Link1.self) { link1 in
    ...
}
.navigationDestination(for: Link2.self) { link2 in
    ...
}
malhal
  • 26,330
  • 7
  • 115
  • 133