1 | initial version |
To respond to the reselection of an item in a SwiftUI Picker, you can use the .onChange()
modifier.
Here's an example:
struct ContentView: View {
@State private var selectedColor = "Red"
var body: some View {
VStack {
Text("Selected color: \(selectedColor)")
Picker("Colors", selection: $selectedColor) {
Text("Red").tag("Red")
Text("Green").tag("Green")
Text("Blue").tag("Blue")
}
.pickerStyle(SegmentedPickerStyle())
.onChange(of: selectedColor) { value in
print("Selected color changed to \(value)")
}
}
}
}
In this example, we have a Picker
with three options, and we're using a segmented picker style.
We're also using the .onChange()
modifier to print a message to the console whenever the selected color changes.
You can replace the print()
statement with any code you want to execute when the selected item is reselected.