Seems to be a bug or behavior quirk in NavigationStack, because if I change the code to use .sheet(), then it suddenly work fine on MacOs:
import SwiftUI
import Observation
let allSelectables: [String] = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]
@Observable
class Model {
var items: Set<String> = [] // Use Set for unique selections
}
struct ContentView: View {
@State var model = Model()
@State private var showSheet = false
var body: some View {
NavigationStack {
VStack {
Text("Selected: \(model.items.joined(separator: ", "))")
List {
Button("Go to Selection (\(model.items.count))") {
showSheet = true
}
}
.sheet(isPresented: $showSheet) {
List(allSelectables, id: \.self, selection: $model.items) { item in
Text(item)
}
#if os(iOS)
.environment(\.editMode, .constant(.active)) // Enable multiple selection
#endif
}
}
}
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI