Change UISegmentedControl selected index or value programmatically

Swift 3.0

segmentedControl.selectedSegmentIndex = #your value#

The correct answer is the @arcady bob one at this link. I have posted an updated version for Swift 5, which I quote here:

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.


From code, you can just do seg.selectedSegmentIndex = someDefaultIndex.

Whether you should set it in viewDidLoad: or not depends entirely on the structure of your application. For example, if your app is starting up and loading the view for the first time and needs to set the control to whatever value it had during the previous run of the app, then it definitely makes sense to do it there.


Change in viewDidLoad:

[seg setSelectedSegmentIndex:index];

you change use the index value as your wish.