How do I switch UISegmentedControl programmatically?

IosIphoneUisegmentedcontrol

Ios Problem Overview


How do I switch UISegmentedControl programmatically?

Ios Solutions


Solution 1 - Ios

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;

Solution 2 - Ios

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)

Solution 3 - Ios

In Swift:

segmentedControl.selectedSegmentIndex = 1

Swift 2:

segmentedControl.sendActionsForControlEvents(UIControlEvents.ValueChanged)

Swift 3:

segmentedControl.sendActions(for: UIControlEvents.valueChanged)

Solution 4 - Ios

@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...
}

Solution 5 - Ios

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.

Solution 6 - Ios

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.

Solution 7 - Ios

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVoloda2View Question on Stackoverflow
Solution 1 - IosLachlan RocheView Answer on Stackoverflow
Solution 2 - Iosarcady bobView Answer on Stackoverflow
Solution 3 - IosMatias ElorriagaView Answer on Stackoverflow
Solution 4 - IosLirikView Answer on Stackoverflow
Solution 5 - Iosnovecento88View Answer on Stackoverflow
Solution 6 - IosDaniel RyanView Answer on Stackoverflow
Solution 7 - IosSharalisha View Answer on Stackoverflow