How do I switch UISegmentedControl
programmatically?
7 Answers
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)

- 7,528
- 9
- 56
- 96

- 1,536
- 1
- 10
- 5
-
13Without `sendActionsForControlEvents`, it just changes the segment but wont trigger event. In Swift it is `segmentedControl.sendActionsForControlEvents(UIControlEvents.ValueChanged)` Thanks a lot. – Stephen Apr 20 '16 at 04:05
-
Working Good but the old index color not changing – Anil Aug 01 '18 at 11:47
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;

- 25,678
- 5
- 79
- 77
-
40This won't actually trigger whatever target/action you have set up for when the user selects a different segment, though. – jmstone617 Jul 11 '12 at 15:27
-
@jmstone617 I wouldn't expect that, just like with a UISwitch - at least in my experience, setting it's value from, say, User Defaults does't automatically call whatever function would be called if the user manually changed its state. – Nov 04 '19 at 06:51
-
to trigger the action you want write afterwards: segmentedControl.sendActions(for: .valueChanged) – Luchi Parejo Alcazar Apr 19 '21 at 14:42
In Swift:
segmentedControl.selectedSegmentIndex = 1
Swift 2:
segmentedControl.sendActionsForControlEvents(UIControlEvents.ValueChanged)
Swift 3:
segmentedControl.sendActions(for: UIControlEvents.valueChanged)

- 8,794
- 4
- 48
- 71

- 8,880
- 5
- 40
- 58
@jmstone response, true, this action will not invoke the valueChanged event for this control.
One way to overcome this is to just call the function yourself:
segmentedControl.selectedSegmentIndex = 3;
[self valueChangedMethod:segmentedControl];
This will call:
- (void)valueChangedMethod:(UISegmentedControl *)segmentedControl
{
//continue your code here...
}

- 3,167
- 1
- 30
- 31
EDIT: as Yogesh Nikam Patil made me notice, UIControlEvents has been renamed from UIControlEvents to UIControl.Event. I've updated my code accordingly.
@arcady bob answer was the one that did the trick for me.
I just post an updated version for SWIFT 5:
yourSegmentedControl.selectedSegmentIndex = 0
yourSegmentedControl.sendActions(for: UIControl.Event.valueChanged)
If you use just the first line, the segmented will react accordingly graphically, but your IBAction associated with the segmented control won't be called. In a few words: the second line is like tapping on the segmented control. This is what I needed and what I was missing.

- 148
- 1
- 2
- 13
I've had a similar problem where the segmented control wasn't changing. I was calling "selectedSegmentIndex", etc too early. Once I called it after "viewDidLoad" was called then everything was fine again.

- 6,976
- 5
- 45
- 62
First, create an outlet from the segmented control you've added from the UI
@IBOutlet weak var segmentedControl: UISegmentedControl! Using the code below programmatically change the selected segment
segmentedControl.selectedSegmentIndex = 1 // index

- 31
- 2