I’m trying to use NavigationLink’s isActive variable to pop back to the root view controller.
The problem I’m experiencing is that using isActive pushes the wrong row when clicking on a list item. Remove the isActive variable and everything works as expected.
Here’s some example code for demonstration purposes:
struct ContentView: View {
@State private var activateNavigationLink: Bool = false
var exampleData = ["a", "b", "c"]
var body: some View {
NavigationView {
List(exampleData, id: .self) { item in
NavigationLink(
destination: SecondView(item: item), isActive: $activateNavigationLink) {
Text(item)
}
}
}
}
}
SecondView
struct SecondView: View {
var item: String
var body: some View {
Text(item)
}
}
This is driving me nuts. Any help would be greatly appreciated.
2
Answers
Because
activateNavigationLink
is just aBool
in your code, if it istrue
, everyNavigationLink
will register as active in yourList
. Right now, this is manifesting as the last item (C
) getting pushed each time.Instead, you’d need some system to store which item is active and then translate that to a boolean binding for the
NavigationLink
to use.Here’s one possible solution:
You should not use activeNavigationLink on main view it should be used with cellView
CellView
SecondView