I am programmatically directing traffic in my app using NavigationLink and isActive.
My primary hierarchy goes like this ContentView -> View 2 -> View 3. And then ContentView -> View 4 (but this part doesn't work).
What I would like to do is pop back to the root ContentView from anywhere and then navigate to a fourth view. Popping back to root is easy, navigating to that fourth view programmatically seems impossible due to a bug in NavigationLink where the fourth view will become active and visible only to immediately go back to ContentView.
Here is my code:
import SwiftUI
@main
struct BugApp: App {
@StateObject var session = Session()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(session)
}
}
}
class Session: ObservableObject {
@Published var showingContentViewTwo: Bool = false
@Published var showContentViewFour: Bool = false
}
struct ContentView: View {
@EnvironmentObject var session: Session
@State private var showingContentViewFour = false
var body: some View {
NavigationView {
VStack {
Spacer()
NavigationLink(destination: ContentViewTwo(), isActive: $session.showingContentViewTwo) {
EmptyView()
}.isDetailLink(false)
NavigationLink(destination: ContentViewFour(), isActive: self.$showingContentViewFour) {
EmptyView()
}.isDetailLink(false)
Text("I am ContentView")
.padding()
HStack {
HStack(alignment: .center) {
Spacer()
Text("Go To View 2")
.foregroundColor(Color.primary)
.bold()
Spacer()
}.padding()
.background(Color.blue)
}
.padding()
.onTapGesture(count: 1) {
session.showingContentViewTwo = true
}
.onReceive(session.$showContentViewFour) { value in
if value {
session.showContentViewFour = false
self.showingContentViewFour = true
}
}
Spacer()
}
}
}
}
struct ContentViewTwo: View {
@EnvironmentObject var session: Session
var body: some View {
VStack {
Spacer()
Text("I am ContentViewTwo")
.padding()
NavigationLink(destination: ContentViewThree()) {
HStack {
HStack(alignment: .center) {
Spacer()
Text("Go To View 3")
.foregroundColor(Color.primary)
.bold()
Spacer()
}.padding()
.background(Color.blue)
}
}
.padding()
Spacer()
}
}
}
struct ContentViewThree: View {
@EnvironmentObject var session: Session
@State private var showingViewFour: Bool = false
var body: some View {
VStack {
NavigationLink(destination: ContentViewFour(), isActive: self.$showingViewFour) {
EmptyView()
}.isDetailLink(false).hidden().allowsHitTesting(false)
Spacer()
Text("I am ContentViewThree")
.padding()
Button(action: {
session.showingContentViewTwo = false
session.showContentViewFour = true
}) {
HStack {
HStack(alignment: .center) {
Spacer()
Text("Go All The Way Back")
.foregroundColor(Color.primary)
.bold()
Spacer()
}.padding()
.background(Color.blue)
}
}
.padding()
Spacer()
}
}
}
struct ContentViewFour: View {
@Environment(\.presentationMode) var presentationMode
@EnvironmentObject var session: Session
var body: some View {
VStack {
Spacer()
Text("I am ContentViewFour")
.padding()
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
HStack {
HStack(alignment: .center) {
Spacer()
Text("Go Back")
.foregroundColor(Color.primary)
.bold()
Spacer()
}.padding()
.background(Color.green)
}
}
.padding()
Spacer()
}
}
}
Help?