10

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.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
  • 2
    How about setting some flag before you set it in code and checking the flag in the action method? If you're not using the control's tag property, you could use that as a flag instead of adding a new bool. –  Dec 31 '10 at 16:24
  • Guys i stuck up with this scenario for 8 hours. And finally end up satisfied here by matt's answer.. Seriously why segmentcontrol? If you too stuck up with this issue try the flag concept specified by Anna.. Thanks a lot to anna and matt... – ipraba Apr 15 '11 at 13:58

3 Answers3

11

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

I still have this issue in iOS 7.1.

I was reloading table and setting segment index but it was calling 'value changed' delegate. To avoid that I nilled 'segment control's' delegate first and then set value programatically, and again reset the delegate to current class. This won't call 'value change' delegate.

BhushanVU
  • 3,455
  • 1
  • 24
  • 33
0

As per Apple

The selectedSegmentIndex property is generally used to determine:

the index number for identifying the selected segment (that is, the last segment touched).

The default value is UISegmentedControlNoSegment (no segment selected) until the user touches a segment. Set this property to -1 to turn off the current selection.

You can also change it to different value depend upon your requirement.

UISegmentedControl ignores this property when the control is in momentary mode. When the user touches a segment to change the selection, the control event UIControlEventValueChanged is generated; if the segmented control is set up to respond to this control event, it sends a action message to its target.

(ref)

Community
  • 1
  • 1
raaz
  • 12,410
  • 22
  • 64
  • 81