Last active
June 21, 2020 08:09
-
-
Save gahntpo/6442b4dc4342b63e88bf3a9640a5cda3 to your computer and use it in GitHub Desktop.
ContentView with NavigationController in the environment
This file contains hidden or 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
import SwiftUI | |
struct ContentView: View { | |
@EnvironmentObject var nav: NavigationController | |
var body: some View { | |
// binding TabView selection to NavigationController | |
TabView(selection: self.$nav.selection){ | |
FirstView() | |
.tabItem { | |
Text("First") | |
}.tag(0) | |
Text("Second View") | |
.font(.title) | |
.tabItem { | |
Text("Second") | |
}.tag(1) | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView().environmentObject(NavigationController()) | |
} | |
} | |
struct FirstView: View { | |
@EnvironmentObject var nav: NavigationController | |
var body: some View { | |
NavigationView { | |
VStack { | |
Text("Main view") | |
.font(.title) | |
// binding NavigationLink isAcitve to NavigationController | |
NavigationLink(destination: FirstDetailView(), isActive: self.$nav.detailIsShown) { | |
Text("show me the details") | |
} | |
} | |
}.navigationViewStyle(StackNavigationViewStyle()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment