I have an integer having value of 5 and I want to start my UIPickerView with this index, now what should I do? normally UIPickerView is on default 0 index row, but I want it on index which is user defined, as can be 5, 6 or any other digit.
Asked
Active
Viewed 1.7k times
2 Answers
47
Assuming your picker is referred to in a property "myPicker", then in viewDidLoad, go:
[self.myPicker selectRow:rowInt inComponent:componentInt animated:NO]

Dan Ray
- 21,623
- 6
- 63
- 87
-
what are rowInt and componentInt ? – Chatar Veer Suthar Feb 17 '11 at 05:01
-
2they're integers saying what row of what component you want to pre-select. – Dan Ray Feb 17 '11 at 13:08
-
what do you mean by inComponent? – Dejell Mar 11 '14 at 16:12
-
1A picker can have more than one "wheel". The default date picker is a good example--it has separate spinner sections for date, hour, minute, etc (depending on how you configure it). In Cocoa-Touch-ese, those are "components". – Dan Ray Mar 12 '14 at 13:42
-
if I want to select first item from picker-view what should pass in parameter – Bhumesh Purohit Apr 12 '18 at 12:02
1
Update for Swift 4.2:
To set the default row in a UIPickerView,
myPicker.selectRow(5, inComponent: 0, animated: true)
where myPicker is the name of the UIPickerView and 5 is the index of the desired row.

Marcy
- 4,611
- 2
- 34
- 52