Search code examples
swiftswiftuiswiftui-navigationstack

How can I have more than one parameter in navigationDestination


Still wrapping my head around the new SwiftUI Navigation... I find it interesting that most (if not all) examples on the web have navigationDestination with one parameter only, like this one from the official docs:

    NavigationStack {
        List(parks) { park in
            NavigationLink(park.name, value: park)
        }
        .navigationDestination(for: Park.self) { park in // <-- here
            ParkDetails(park: park) // <-- and here
        }
    }

What if my ParkDetails accepts more than one param? Like if I need ParkDetails(park: park, updateThumbnail: true)? Is there a proven (and easy) way to do it? Should I use a touple (Park, Bool)?

Previously I used (and it worked flawlessly):

NavigationLink(destination: ParkDetails(park: park, updateThumbnail: true)) {
    //Link here
}

P.S. While I need only two parameters in my case, what if I needed 15 different params and some of them would be optional? Would I create a gazillion different .navigationDestination()'s?


Solution

  • Store your additional info on the Navigation Enum

    enum Park: Hashable {
      case park1(Bool) // updateThumbnail
    }
    

    Read about enum associated values here in the docs https://docs.swift.org/swift-book/documentation/the-swift-programming-language/enumerations/#Associated-Values