Consider the following PreferencesView presented as a sheet:
struct PreferencesView: View {
@Environment(\.dismiss) private var dismiss
@AppStorage("setting1") var setting1: Bool = true
@AppStorage("setting2") var setting2: Bool = false
@AppStorage("setting3") var setting3: Bool = false
// many more
var body: some View {
NavigationView {
Form {
Toggle("Setting 1", isOn: $setting1)
Toggle("Setting 2", isOn: $setting2)
Toggle("Setting 3", isOn: $setting3)
}
.navigationTitle("Preferences")
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button("Cancel") {
reset()
dismiss()
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button("Save") {
dismiss()
}
}
}
}
}
private func reset() {
// what do I put here?
}
}
If the user hits cancel, I'd like the value of the settings be set back to when the sheet opened. I could add separate initialSetting1, etc properties, and then in the reset function add setting1 = initialSetting1. But with a lot of settings that becomes tedious.
Is there another way?
onAppear@Stateto bind to a toggle@Stateto settings when user presses Save button.Like this: