Change UISegmentedControl selected index or value programmatically Change UISegmentedControl selected index or value programmatically xcode xcode

Change UISegmentedControl selected index or value programmatically


Change in viewDidLoad:

[seg setSelectedSegmentIndex:index];

you change use the index value as your wish.


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.


Swift 3.0

segmentedControl.selectedSegmentIndex = #your value#