I'm working on a SwiftUI app that includes an asset manager that allows users to import and edit custom assets. When browsing available assets, users can choose to view the assets in a List
view that is populated by a SectionedFetchRequest.
Each row of the list is a NavigationLink
that presents a detail view for the asset that the user tapped on. I've got all of this working nicely so far.
The issue I'm running into now is adding multi-select support to the list so users can select and modify multiple assets at once. When I add the selection:
parameter to the list, the NavigationLink
in each row no longer presents a detail view when tapped.
Below is a very simplified example of what I'm trying to accomplish:
struct TestView: View {
let fruits = ["Apple", "Banana", "Cherry"]
@State private var selection: Set<String> = Set()
var body: some View {
NavigationStack {
List(fruits, id: \.self, selection: $selection) { fruit in
NavigationLink(fruit, value: fruit)
}
.navigationDestination(for: String.self) { string in
Text(string)
}
.toolbar {
EditButton()
}
}
}
}
However, I noticed that by using a NavigationLink
initializer other than .init(_:value:)
, such as .init(_:destination:)
, the list works as expected. That is, NavigationLink
still presents a detail view when tapped and the list supports multi-select. Again, here's a simplified view:
struct TestView: View {
let fruits = ["Apple", "Banana", "Cherry"]
@State private var selection: Set<String> = Set()
var body: some View {
NavigationStack {
List(fruits, id: \.self, selection: $selection) { fruit in
NavigationLink(fruit) {
Text(fruit)
}
}
.toolbar {
EditButton()
}
}
}
}
Is this a known bug or is there something else I'm missing? I've tried finding information on this, but have come up short.