That is on Xcode 14.2 and iOS 16.2
I am engaged on a SwiftUI app with nested lists and navigation. I’ve written out a fundamental instance demoing the problem. The construction is as follows:
- The principle view has a listing of things (sort Merchandise).
- Clicking on an merchandise navigates to a element view.
- The element view has a button that navigates to a different listing of things (similar sort Merchandise).
- Whenever you navigate to this listing, one of many gadgets is already highlighted/chosen (presumably as a result of it’s inheriting the choice from the earlier listing??)
Code that demo’s the problem
import SwiftUI
struct Merchandise: Hashable, Identifiable {
let id: Int
let factor: String
}
struct TopLevel: View {
var physique: some View {
NavigationStack {
// uncomment one or the opposite to check
Programmatic_NavLink() // I want to make use of this, nevertheless it has this bizarre choice habits/bug??
//Old_NavLink() // this does what I would like however I want to have the ability to use worth based mostly navigation
}
}
}
struct Programmatic_NavLink: View {
let myItems = [Item(id: 1, thing: "thing 1"), Item(id: 2, thing: "thing 2"), Item(id: 3, thing: "thing 3")]
var physique: some View {
Listing(myItems) { merchandise in
NavigationLink(merchandise.factor, worth: merchandise)
}
.navigationDestination(for: Merchandise.self) { merchandise in
Particulars(model: "programmatic")
}
.navigationDestination(for: Int.self) { num in
Programmatic_NavLink()
}
}
}
struct Old_NavLink: View {
let myItems = [Item(id: 1, thing: "thing 1"), Item(id: 2, thing: "thing 2"), Item(id: 3, thing: "thing 3")]
var physique: some View {
Listing(myItems) { merchandise in
NavigationLink(merchandise.factor) {
Particulars(model: "traditional")
}
}
}
}
struct Particulars: View {
let model: String
var physique: some View {
if model == "programmatic" {
NavigationLink("Demo", worth: 5)
} else {
NavigationLink("Demo", vacation spot: Old_NavLink())
}
}
}
struct TestNavStack_Previews: PreviewProvider {
static var previews: some View {
TopLevel()
}
}
In case you use the traditional navigation hyperlink, it doesn’t behave this fashion. I wish to have the lists be utterly unbiased of one another although they could comprise one merchandise that’s the similar as one from a earlier listing. I’ve tried including a $choice enter to my listing that’s explicitly set to nil, however this has the aspect impact of constructing the navigation hyperlinks utterly not operate. I’ve additionally tried passing bindings explicitly set to nil to make use of as the choice worth, nevertheless it has the identical problem. I’d simply use the traditional navigation hyperlink api, however I want to have the ability to use the worth based mostly navigation hyperlink for different causes.
Anybody have any concepts methods to repair this? Is that this a bug?