Skip to content

Instantly share code, notes, and snippets.

@w-i-n-s
Last active Jun 16, 2021
Embed
What would you like to do?
SwiftUI Multidestination NavigationLink
// Edited by me. Cos I need custom back button
// Origin https://www.hackingwithswift.com/articles/216/complete-guide-to-navigationview-in-swiftui
// Adult router https://functionalgibberish.com/2020-06-03-RoutingInSwiftUI/
enum NavigationTag {
case Second
case Third
}
struct ContentView: View {
@State private var selection: NavigationTag?
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: Text("Second View"),
tag: NavigationTag.Second,
selection: $selection) { EmptyView() }
NavigationLink(destination: ThirdView(route: $selection),
tag: NavigationTag.Third,
selection: $selection) { EmptyView() }
Button("Tap to show second") {
self.selection = NavigationTag.Second
}
Button("Tap to show third") {
self.selection = NavigationTag.Third
}
}
.navigationTitle("Navigation")
}
}
}
struct ThirdView: View {
@Binding var route: NavigationTag?
var body: some View {
VStack {
Text("Second View")
Button(action: {
route = nil
}, label: {
Text("Back")
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment