Last active
April 15, 2020 01:22
-
-
Save mahmudahsan/544278d334941983a0e72ea65a0f170d to your computer and use it in GitHub Desktop.
SwiftUI: How to Pop to TabView From NavigationView #1 https://thinkdiff.net/ios/swiftui-how-to-pop-to-tabview-from-navigationview/
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
import SwiftUI | |
struct ContentView: View { | |
@EnvironmentObject var appState: AppState | |
var body: some View { | |
NavigationView { | |
VStack { | |
// TabView1() | |
tabsView | |
} | |
} | |
} | |
var tabsView: some View { | |
TabView { | |
TabView1() | |
.tabItem({ | |
Image(systemName: "flame") | |
Text("Fire") | |
}).tag(1) | |
TabView2() | |
.tabItem({ | |
Image(systemName: "umbrella") | |
Text("Rain") | |
}).tag(2) | |
} | |
} | |
} | |
// MARK:- Tab View 1 | |
struct TabView1: View { | |
@EnvironmentObject var appState: AppState | |
@State var isView1Active: Bool = false | |
var body: some View { | |
VStack { | |
Text("Tab View 1") | |
.font(.headline) | |
NavigationLink(destination: View1(), isActive: $isView1Active) { | |
Text("View 1") | |
.font(.headline) | |
} | |
.isDetailLink(false) | |
} | |
} | |
} | |
// MARK:- Tab View 2 | |
struct TabView2: View { | |
var body: some View { | |
VStack { | |
Text("Tab View 2") | |
.font(.headline) | |
} | |
} | |
} | |
// MARK:- View 1 | |
struct View1: View { | |
var body: some View { | |
VStack { | |
Text("View 1") | |
.font(.headline) | |
NavigationLink(destination: View2()) { | |
Text("View 2") | |
.font(.headline) | |
} | |
} | |
} | |
} | |
// MARK:- View 2 | |
struct View2: View { | |
@EnvironmentObject var appState: AppState | |
var body: some View { | |
VStack { | |
Text("View 2") | |
.font(.headline) | |
Button(action: { | |
// FIXME:- add move to dashboard functionality | |
}) { | |
Text("Move to Dashboard") | |
.font(.headline) | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment