I have this code example that works perfectly on iOS. But on MacOS, the changes made in view presented by Navigation Stack gets reverted after I click on Back button.
import SwiftUI
import Observation
enum Route: Hashable {
case selection
}
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()
var body: some View {
NavigationStack {
VStack {
Text("Selected: \(model.items.joined(separator: ", "))")
List {
NavigationLink(value: Route.selection) {
Text("Go to Selection (\(model.items.count))")
}
}
.navigationDestination(for: Route.self) { route in
switch route {
case .selection:
List(allSelectables, id: \.self, selection: $model.items) { item in
Text(item)
}
#if os(iOS)
.environment(\.editMode, .constant(.active)) // Enable multiple selection
#endif
.navigationTitle("Selected: \(model.items.joined(separator: ", "))")
}
}
}
}
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI