Skip to content

Instantly share code, notes, and snippets.

@w-i-n-s
Last active June 16, 2021 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w-i-n-s/b280ee9c6ae722e2b9965ed3f8466251 to your computer and use it in GitHub Desktop.
Save w-i-n-s/b280ee9c6ae722e2b9965ed3f8466251 to your computer and use it in GitHub Desktop.
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