Advanced Navigation Destinations in SwiftUI
A set of powerful techniques that might just change how you view navigation in SwiftUI. Forever.
Two years ago Apple revamped navigation in iOS 16 with the introduction of NavigationStack
, NavigationLink(value:label:)
, NavigationPath
, and the navigationDestination
modifer.
While NavigationStack
and NavigationPath
opened up the door to imperative, programatic navigation techniques, I’m of the option that NavigationLink value was the true game changer.
With NavigationLink value you no longer had to deal with a view explicitly defining its destination inside of each and every NavigationLink.
NavigationLink("View Account") {
AccountDetailView(viewModel: AccountDetailsViewModel(account: selectedAccount))
}
Nor did you have to worry about overly eager evaluations of the destination view that lead so many of us astray. Just “push” a value onto the navigation stack…
List {
NavigationLink("View Account", value: selectedAccount)
}
And a navigationDestination
handler specified somewhere in the code will match the type of the pushed value and create the desired view for you.
.navigationDestination(Account.self) { account in…