I'm trying to push views programmatically in SwiftUI, but when I push two views using the isActive property of NavigationLink the last view is pushed and instantly popped. I'm not sure if I'm doing something wrong or if this is a bug with SwuiftUI.
This is how it looks when I try to push the two views:
If I push one view and then the other some times it works:
This is my code. I'm passing appState as an EnvironmentObject
class AppState: ObservableObject {
@Published var showView1 = false
@Published var showView2 = false
}
struct ContentView: View {
@EnvironmentObject var appState: AppState
var body: some View {
NavigationView {
VStack {
Button(action: {
appState.showView2 = false
appState.showView1 = true
}) {
Text("Show view 1")
}
Button(action: {
appState.showView2 = true
appState.showView1 = true
}) {
Text("Show view 2")
}
NavigationLink( destination: View1(), isActive: $appState.showView1, label: { EmptyView() })
}
}
}
}
struct View1: View {
@EnvironmentObject var appState: AppState
var body: some View {
VStack {
Text("This is view1")
Button(action: {
appState.showView2 = true
}) {
Text("Show view2")
}
NavigationLink( destination: View2(), isActive: $appState.showView2, label: { EmptyView() })
}
}
}
struct View2: View {
@EnvironmentObject var appState: AppState
var body: some View {
VStack {
Text("This is view2")
Button(action: {
appState.showView1 = false
appState.showView2 = false
}) {
Text("Back to root")
}
}
}
}
This may be related to the bug described here https://stackoverflow.com/a/57274613/2584078
But in this case is the opposite. The view is popped insted of pushed
I'm using Xcode 12.0 beta 2 (12A6163b)