Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone SDK: How to set the initial value of a UIPickerView?

Tags:

iphone

I have a picker view that has a list of numbers to pick from. I want to be able to initialize the control at a particular row. For example, if the user specifies 10 as their default, then the control should be automatically positioned at that row when the control is first initialized.

Thanks in advance.

like image 357
jp chance Avatar asked Oct 28 '09 19:10

jp chance


1 Answers

You call selectRow:inComponent:animated: and pass it the index of the row you want selected.

This code causes a UIPickerView to spin through its numbers 0 to 60 before coming to rest on 0.

- (void)viewDidAppear:(BOOL)animated {

    [thePicker selectRow:60 inComponent:0 animated:YES];
    [thePicker reloadComponent:0];
    [thePicker selectRow:60 inComponent:1 animated:YES];
    [thePicker reloadComponent:1];
    [thePicker selectRow:0 inComponent:0 animated:YES];
    [thePicker reloadComponent:0];
    [thePicker selectRow:0 inComponent:1 animated:YES];
    [thePicker reloadComponent:1];
}

In your case, to display row ten you would call something like:

[thePicker selectRow:10 inComponent:0 animated:YES];
like image 155
TechZen Avatar answered Nov 15 '22 14:11

TechZen