7

This is probably a really simple question but I can't seem to find anything in the APIs or across any search engine.

I have a Segmented control that i have set to momentary as a user will select a couple of makes of a car that they want to search for. The issue that I'm running into is that I can't seem to figure out how to recognize which segment was selected. In regular mode its a simple SelectedSegment = index but with momentary its my understanding that the selected segment is always -1 as none are ever "selected"

I have a handler for ValueChanged but I can't figure out what I'm checking for or what I should be sending to determine which segment was selected. Any help would be greatly appreciated. I'm using monotouch but Obj-C would be fine as well.

Thanks!

Adam
  • 152
  • 8

1 Answers1

10

In your handler, you should check the selectedSegmentIndex to determine which segment was selected:

- (void)valueChanged:(UISegmentedControl *) control {
   switch([control selectedSegmentIndex]) {
      case 0:
         //...
         break;
      case 1:
         //...
         break;
   }
}
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • 2
    This is correct. The UISegmentedControl only tracks the *last* item that was selected, not *all* currently selected items. You'll have to maintain the currently selected list in your own object. – John Franklin Jul 08 '10 at 17:48
  • 3
    Wow, that does work. The [Apple documentation](http://developer.apple.com/library/ios/documentation/uikit/reference/UISegmentedControl_Class/Reference/UISegmentedControl.html#//apple_ref/occ/instp/UISegmentedControl/selectedSegmentIndex) seems to indicate it would not. "UISegmentedControl ignores this property when the control is in momentary mode." But selectedSegmentIndex does produce a good value, so works for me. – Pat McG Jul 01 '13 at 00:17