I'm setting the selectedSegmentIndex of an UISegmentedControl via code.
Whenever I do this the valueChanged action gets called. This sounds logical for me, but is there a way to set the selected segment without invoking the action? It should just update the display.
I've used UISegmentedControl more than once, and until now I didn't even noticed that behavior. But this time I need to present an alert if a special segment is selected. So I can't live with the invoked action if the view appears and I want to show the previous selected value.
I could disconnect the action, change the selectedValue and reconnect the action. But maybe there is a better way.
Your question is a sensible one; I can only suggest that you've found a bug, since this is so unusual. Usually, changing a control in code doesn't cause any control events to be emitted. Setting a UIDatePicker's date
doesn't emit a Value Changed event. Setting a UIPageControl's currentPage
doesn't emit a Value Changed event. Setting a UISlider's value
doesn't emit a Value Changed event. Setting a UISwitch's on
doesn't emit a Value Changed event. Similarly, setting a UITextField's text
doesn't emit an Editing Changed event. So, the fact that changing a UISegmentedControl's selectedSegmentIndex
emits a Value Changed event feels wrong. I'm going to file a bug and I think you should too.
I don't see an obvious way to determine whether the Value Changed event was triggered by the user tapping or programmatically. You'll have to raise and lower a BOOL flag or something.
EDIT: This is fixed in iOS 5.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With