John
John

Reputation: 133

Xcode: How To Make PickerView Start At A Certain Row When It Is First Displayed

Is there a way to make the PickerView in Xcode start at a default row? Right now, my PickerView starts at the first element in my array.

So for example, if I have 1 component with 30 rows, how can I make the PickerView start at row 15 when the user first sees the PickerView?

Thanks

Upvotes: 5

Views: 2701

Answers (2)

Shane
Shane

Reputation: 49

You have to assign a variable named as selectedrow which have default value 0 in your viewDidLoad. i.e. int seletedrow=0;

After that do following, in the didSelectRow method of UIPickerView:

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:    (NSInteger)component
{

   selectedrow=row;

}

And at last write following code where you are initialising PickerView:

_pickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 43, 320, 480)];
     _pickerView.delegate=self;
    _pickerView.dataSource=self;
    _pickerView.backgroundColor=[UIColor whiteColor];
    [_pickerView selectRow:selectedrow inComponent:0 animated:NO];
    [_pickerView setShowsSelectionIndicator:YES];     

Upvotes: 0

pho0
pho0

Reputation: 1751

use -[UIPickerView selectRow:inComponent:animated:] ... assuming you have one component, do the following:

UIPickerView *aPicker = [[[UIPickerView alloc] init] autorelease];
aPicker.delegate = self;
aPicker.dataSource = self;
aPicker.showsSelectionIndicator = YES;
[self.view addSubview:aPicker];
[aPicker selectRow:14 inComponent:0 animated:NO];

Upvotes: 9

Related Questions