15

I'm using a 3 button UISegmented control for a choice selection. I also have a save button that retrieves the chosen control.

When the save button is clicked I want to have to UISegmentedcontrol cleared (ie the previous selected button unselected). I'm not looking for the setMomentary as I want the selection to stick but also be able to unselect it later.

Ayrad
  • 3,996
  • 8
  • 45
  • 86

3 Answers3

44
[myUISegmentedControl setSelectedSegmentIndex:UISegmentedControlNoSegment];
lelot
  • 596
  • 1
  • 5
  • 6
24
myUISegmentedControl.selectedSegmentIndex = -1; //turn off the current selection
Tomasz Wojtkowiak
  • 4,910
  • 1
  • 28
  • 35
4

With Swift 4 and iOS 11, the Apple documentation states for selectedSegmentIndex:

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.


Therefore you can use one of the two following implementations in order to remove the selection of your UISegmentedControl instance:

mySegmentedControl.selectedSegmentIndex = -1
mySegmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment
Imanou Petit
  • 89,880
  • 29
  • 256
  • 218