Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I switch UISegmentedControl programmatically?

How do I switch UISegmentedControl programmatically?

like image 448
Voloda2 Avatar asked Mar 11 '11 13:03

Voloda2


2 Answers

The selectedSegmentIndex property identifies the selected segment of a UISegmentedControl. Set this property to the any valid segment index, or UISegmentedControlNoSegment (-1) to turn off the current selection.

// select the first segment
segmented.selectedSegmentIndex = 0;

// turn off the current selection
segmented.selectedSegmentIndex = UISegmentedControlNoSegment;
like image 107
Lachlan Roche Avatar answered Nov 18 '22 01:11

Lachlan Roche


Alternatively, after you have changed the selectedSegmentIndex call 'sendActionsForControlEvents:' for example

segmentedControl.selectedSegmentIndex = 0

[segmentedControl sendActionsForControlEvents:UIControlEventValueChanged];

SWIFT 5:

segmentedControl.selectedSegmentIndex = 0
segmentedControl.sendActions(for: .valueChanged)
like image 103
arcady bob Avatar answered Nov 18 '22 02:11

arcady bob