Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISegmentedControl deselect (make none of the segments selected)

in fact the title contains my question. I have a UISegmentedControl, and need to deselect currently selected tab. I tried:

[menu setSelectedSegmentIndex:-1]; 

menu being the UBOutlet for uisegmentedcontrol but this gives me exception. anyone have some idea? thanks peter

like image 481
dusker Avatar asked Feb 05 '10 09:02

dusker


2 Answers

The right way to do this is:

[menu setSelectedSegmentIndex:UISegmentedControlNoSegment]; 

Edit:

Swift 5:

menu.selectedSegmentIndex = UISegmentedControl.noSegment 
like image 51
Calin Drule Avatar answered Sep 23 '22 03:09

Calin Drule


Setting the segmentedControl to .momentary = YES changes the way the user can interact with the segmentedControl. If you want to have nothing selected initially when the page loads but have segmentedControl behave the same after a selection is made by the user then you will want something more like this:

Swift 4 solution:

 @IBOutlet weak var segmentedControl: UISegmentedControl!   self.segmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment 

Swift 5.1:

 self.segmentedControl.selectedSegmentIndex = UISegmentedControl.noSegment 
like image 30
Dan Leonard Avatar answered Sep 23 '22 03:09

Dan Leonard