You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've had a look at implementing this myself with a subclass of Picker however I've discovered the way it's implemented at the shared level creates a doom loop of property changes. Firstly I will describe the scenario:
Picture a Picker with 4 options, you allow the user to choose any option however you want to prompt them before they choose a certain option: Hot Sauce Level
Mild
Spicy
Hot
This Will Blow Your Head Off, Do Not Eat
Now imagine in this scenario the user chooses option 4, the spiciest hot sauce. What if we wanted to show a further prompt to the user to see if this is really what they want...
Here is such a demo running in SwiftUI, this is a fairly simple concept to throw together:
structValidatedPickerView:View{letpickerOptions=["Mild","Spicy","Hot","Blow Your Head Off Hot"]@Stateprivatevarselection:String="Mild"@StateprivatevarlastValidSelection:String="Mild"@StateprivatevarpendingConfirmation=falsevarbody:someView{VStack(spacing:20){Text("Selected: \(selection)").font(.headline)Picker("Choose your heat level", selection: $selection){ForEach(pickerOptions, id: \.self){ option inText(option)}}.pickerStyle(WheelPickerStyle()).onChange(of: selection){ newValue inif newValue =="Blow Your Head Off Hot"{
pendingConfirmation =true}else{
lastValidSelection = newValue
}}}.alert("Are you sure you want this much heat?", isPresented: $pendingConfirmation){Button("Yes, I'm ready 🔥"){
lastValidSelection ="Blow Your Head Off Hot"}Button("Actually no", role:.cancel){
selection = lastValidSelection
}} message:{Text("“Blow Your Head Off Hot” is not for the faint of heart.")}.padding()}}
Here is this sample running on a device, quite a nifty way of providing a good UX
hot.sauce.picker.test.mov
Unfortunately as I stated at the beginning, this doesn't seem to be possible to do with Picker since after a value is confirmed we need to set the previous index. Any attempt I've made has entered a doom loop of infinite property changes, hard locking the app until it is forceably closed.
Confirming a value would probably want to be done asynchronously, perhaps a deferral token could also be used in this case. A way I can think of allowing this to be written would be allowing the override of OnSelectedIndexChanged so the ConfirmationPicker could handle this logic before initiating the changes. I'm not sure exactly what the best implementation strategy would be, however I do see this as a valuable enhancement to Picker especially given the fact that currently it does not work at least on iOS.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I've had a look at implementing this myself with a subclass of
Picker
however I've discovered the way it's implemented at the shared level creates a doom loop of property changes. Firstly I will describe the scenario:Picture a Picker with 4 options, you allow the user to choose any option however you want to prompt them before they choose a certain option:
Hot Sauce Level
Now imagine in this scenario the user chooses option 4, the spiciest hot sauce. What if we wanted to show a further prompt to the user to see if this is really what they want...
Here is such a demo running in SwiftUI, this is a fairly simple concept to throw together:
Here is this sample running on a device, quite a nifty way of providing a good UX
hot.sauce.picker.test.mov
Unfortunately as I stated at the beginning, this doesn't seem to be possible to do with
Picker
since after a value is confirmed we need to set the previous index. Any attempt I've made has entered a doom loop of infinite property changes, hard locking the app until it is forceably closed.Here is an example of the logic:
Confirming a value would probably want to be done asynchronously, perhaps a deferral token could also be used in this case. A way I can think of allowing this to be written would be allowing the override of
OnSelectedIndexChanged
so theConfirmationPicker
could handle this logic before initiating the changes. I'm not sure exactly what the best implementation strategy would be, however I do see this as a valuable enhancement to Picker especially given the fact that currently it does not work at least on iOS.Beta Was this translation helpful? Give feedback.
All reactions