Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the default state of a UISegmentedControl?

Is there a way to set the starting selected segment in a UISegmentedControl in Interface Builder, or do I have to do it in the code? If it's in the code, is viewDidLoad the best place to set it?

like image 413
nevan king Avatar asked Jun 22 '09 14:06

nevan king


People also ask

How do you get the selected segment title in Swift?

Enter Swift as Language and choose Next. Go to the Storyboard and drag a Segmented Control to the top of the main view. Also drag a Label to the view and place it below the Segmented Control. Select the label and give it a text of First Segment selected.

What is segmented control?

A segmented control is a linear set of two or more segments, each of which functions as a button. Within a segmented control, all segments are usually equal in width. Like buttons, segments can contain text or images. Segments can also have text labels beneath them (or beneath the control as a whole).


3 Answers

From code, you can do:

self.segmentedControl.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.

like image 146
drewh Avatar answered Oct 08 '22 08:10

drewh


In Interface Builder when you select UISegmentedControl object on your UI, then in attributes pane, in segment control there's segment drop down menu, select segment that you want selected (0,1 and so on) and tick the 'selected' option below it.

like image 20
stefanB Avatar answered Oct 08 '22 07:10

stefanB


Select default value for your UISegmentedControl via storyBoard

  1. Open ur storyboard and select the UISegmentedControl

enter image description here

  1. Select atributes inspector of your UISegmentedControl (in the right corner)

enter image description here

  1. Search the 'segment' atribute and open de dropdown.

enter image description here

  1. Select the item you want to be selected by default.

enter image description here

  1. Mark the selected checkbox to set selected by default.

enter image description here

like image 2
Iker Solozabal Avatar answered Oct 08 '22 08:10

Iker Solozabal