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

2

5

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

Serpens answered 23/2, 2012 at 0:9 Comment(0)
T
9

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];
Theresa answered 23/2, 2012 at 0:20 Comment(0)
S
0

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];     
Sequestered answered 18/9, 2014 at 6:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.