UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Pop multiple nested views using NavigationLink and isActive

Forums > SwiftUI

Hi, I am trying to pop multiple screens back using the isActive property of NavigationLink.

Here is an example:

struct ViewA {
    @State private var showNestedView = false

    var body: some View {
        VStack {
            Text("ViewA")
            Button(action: { showNestedView = true }) {
                Text("Open ViewB")
            }

            NavigationLink(destination: VStack {
                Text("ViewB")
                NavigationLink("Open ViewC", destination: VStack {
                    Text("ViewC")
                    Button(action: { showNestedView = false}) {
                        Text("Go back to ViewA")
                    }
                })
            }, isActive: $showNestedView) {
                EmptyView()
            }
    }
}

The gist of it is that: ViewA opens ViewB using a NavigationLink and isActive: $showNestedView. ViewB then opens ViewC. ViewC has a button that changes the value of showNestedView to false. I expect that this will close both ViewC and ViewB and ViewA will apear on the screen again. Instread ViewC gets clicked the state of showNestedView changes and nothing else happens.

1      

TLDR:
I had to set NavigationLink(...).isDetailLink(false) in order to be able to pop multiple views.

Long Answer:
I did play around with this example here: https://www.reddit.com/r/SwiftUI/comments/o86wqu/comment/h33juip/?utm_source=share&utm_medium=web2x&context=3

At some point I ended up having a set of NavigationLinks where the third view that was pushed into the stack was immediately poped after rendering. Then I ended up on this thread: https://developer.apple.com/forums/thread/677333?login=true&page=4#709740022 Setting isDetailLink(false) fixed both problems.

Also the example in the question doesn't fully demonstrate the problem.

Here is a more accurate example

struct ViewX: View {
    @Binding var showNestedView: Bool

    var body: some View {
        VStack{
            Text("ViewX")

            Button(action: { showNestedView = true }) {
                Text("Open ViewY")
            }

            NavigationLink(destination: ViewY(showNestedView: $showNestedView), isActive: $showNestedView) {
                EmptyView()
            }.isDetailLink(false)
        }
    }
}

struct ViewY: View {
    @Binding var showNestedView: Bool

    var body: some View {
        VStack {
            Text("ViewY")
            NavigationLink("Open ViewZ", destination: ViewZ(showNestedView: $showNestedView))
                .isDetailLink(false)
        }
    }
}

struct ViewZ: View {
    @Binding var showNestedView: Bool
    @State var showNestedView2: Bool = false

    var body: some View {
        VStack {
            Text("ViewZ")
            Button(action: { showNestedView = false}) {
                Text("Go back to ViewX")
            }

            NavigationLink("Open ViewX again", destination: ViewX(showNestedView: $showNestedView2))
                .isDetailLink(false)
        }
    }
}

The problem starts occurring when the same view appears multiple times on the stack. In this case ViewZ can open ViewX again. In which case popping back from ViewZ to ViewX stops working unless isDetailLink(false) is used.

1      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.