2

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:

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:

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)

Juan
  • 147
  • 3
  • 6
  • Does this answer your question? [SwiftUI: How to pop to Root view](https://stackoverflow.com/questions/57334455/swiftui-how-to-pop-to-root-view) – pawello2222 Jul 09 '20 at 19:22
  • It looks like `$appState.showView1` is always true. Should you set it to false when you navigate to view2? You may also want to consider a different method for handling navigation state. I don't imagine this method would be very manageable with 10 or more states. – CodeShaman Jul 09 '20 at 19:25
  • @pawello2222 No. The problem is not about going back to the root view (this actually works fine). Is about "pushing" multiple views. – Juan Jul 09 '20 at 19:56
  • @CodeShaman I think `appState.showView1` needs to be true when showing a view on top of it. Other ways because the View2 is a child of View1, View2 will never show and instead it will pop to the root view. I agree, this method will probably don't scale too well, but is more like a proof of concept to know what I can count on when building more complex navigations. – Juan Jul 09 '20 at 20:00

0 Answers0