Last active
June 16, 2021 20:52
-
-
Save w-i-n-s/b280ee9c6ae722e2b9965ed3f8466251 to your computer and use it in GitHub Desktop.
SwiftUI Multidestination NavigationLink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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