SwiftUI Navigation link issue

When I am using Navigation link from a ForEachLoop I see "SwiftUI encountered an issue when pushing aNavigationLink. Please file a bug.
Unable to present. Please file a bug."

When I try to select a row, all the rows are being selected.

I used breakpoint in the destination view, I see multiple times init is getting called.

Example code:
struct ExampleHeaderView: View {

    var sectionRows: [SectionRows]

    @State var isActive = false

    @State var isNavBarHidden = false

    

    var body: some View {

        ForEach(sectionRows) { sectionRow in

            if let sectionType = sectionRow.sectionType {

                switch sectionType {

                case "Ex3":

                    VStack {

                        NavigationLink(destination: ExampleView(pathQuery: sectionRow.path ??  "", isActive: self.$isActive)

                                        .navigationBarHidden(self.isNavBarHidden)

                                        .onAppear {

                                            self.isNavBarHidden = true

                                        }.onDisappear {

                                            self.isNavBarHidden = false

                                        }

                                        .navigationBarTitle("")

                                        .navigationBarBackButtonHidden(true), isActive: $isActive) {

                            ExampleRowView(sectionRow: sectionRow)

                        }

                        .padding([.trailing, .leading], 16.0)

                        Divider()

                    }

                default:

                   EmptyView()

                }

            }

        }

    }

}
Hi,
I would put the ForEach in either a List, or a form. Then you should change the row to something like this:
Code Block swift
  NavigationLink(destination: Text("Destination")){
        VStack{
            Text("Content")
        }
    }

The NavigationLink should be the first thing in the row to work probably.

Take care,
David

I'm having the same issue
I can see why you have that problem.
Code Block SwiftUI
NavigationLink(destination: ExampleView(), isActive: self.$isActive) {}

When you create a NavigationLink with the parameter isActive, you link that instance of NavigationLink to the state variable. When the value of the variables changes, all correlating views are updated, including NavigationLink.

Basically, pressing on the NavigationLink changes the value of isActive to true, and so every other NavigationLink, which is connected to the variable, also gets the information, that it should push a new view.

The answer would be to remove that entire state variable and add this variable in the destination view:
Code Block SwiftUI
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

And then use that method to dismiss current view:
Code Block SwiftUI
presentationMode.wrappedValue.dismiss()


Dear St1fF,

Thank you for your answer. I have a similar problem here.

May I ask where do you put presentationMode.wrappedValue.dismiss()?

Cheers,


@St1f

I'am having the same issue, but in my case I'm using the "init(destination:tag:selection:label:)" init.

I have s state var where I'm keeping the selection, and multiple (a list) of links.
Basically I'm displaying tappable search results.

Any suggestions?
The problem is that you have NavigationLink placed in the ForEach cycle! You need to remove NavigationLink from the cycle and transfer the necessary data there, for example, through the view model.

Summary: you must have only one NavigationLink with the "IsActive" parameter.

My original answer
SwiftUI Navigation link issue
 
 
Q