82

How do I switch UISegmentedControl programmatically?

pkamb
  • 33,281
  • 23
  • 160
  • 191
Voloda2
  • 12,359
  • 18
  • 80
  • 130

7 Answers7

108

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)
SwiftiSwift
  • 7,528
  • 9
  • 56
  • 96
arcady bob
  • 1,536
  • 1
  • 10
  • 5
  • 13
    Without `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
100

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;
Lachlan Roche
  • 25,678
  • 5
  • 79
  • 77
  • 40
    This 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
29

In Swift:

segmentedControl.selectedSegmentIndex = 1

Swift 2:

segmentedControl.sendActionsForControlEvents(UIControlEvents.ValueChanged)

Swift 3:

segmentedControl.sendActions(for: UIControlEvents.valueChanged)
Daniel
  • 8,794
  • 4
  • 48
  • 71
Matias Elorriaga
  • 8,880
  • 5
  • 40
  • 58
25

@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...
}
Lirik
  • 3,167
  • 1
  • 30
  • 31
7

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.

EmmettBrown88
  • 148
  • 1
  • 2
  • 13
3

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.

Daniel Ryan
  • 6,976
  • 5
  • 45
  • 62
3

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