Hello I have made a picker in swiftUI where the user has to accept the change on the picker.
It works well, but there is one issue. If the user selects something and then declines the change, the shown value will still be what was selected though the actual value doesn't change.
If I open the picker again without selecting anything it reverts back to the right value but how to I do this without having to open the picker?
struct ContentView: View {
@State private var selectedColorIndex = "cd"
@State private var temp = ""
@State var alert = false
private var colors = ["ab", "cd", "ef", "gh", "ij", "kl", "mn", "op", "qr", "st", "uv", "wx", "yz"]
var body: some View {
HStack {
Text("Location")
Picker("Location", selection: Binding(get: {selectedColorIndex}, set: {
temp = $0
alert = true
})) {
ForEach(colors, id: \.self) {
Text($0)
}
}.pickerStyle(MenuPickerStyle())
}.alert(isPresented: $alert) {
Alert(title: Text("are you sure"), primaryButton: .default(Text("yes"), action: {
selectedColorIndex = temp
}), secondaryButton: .default(Text("no"), action: {
//change shown value back to prev selectedColorIndex
}))
}
}
}