Mahesh Babu
Mahesh Babu

Reputation: 3433

how to place previously selected value at UIPickerview bar

I am displaying my array objects in a pickerview.

My array contains {one,two,three,four,five}

When I select a value in picker view, I am getting that value using
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

If I select two, exit from the picker view and for next time it display one in picker view bar.

But I need to display previously selected value.

I think this is understandable otherwise let me add comment.

Upvotes: 1

Views: 1614

Answers (4)

Ilias Alexopoulos
Ilias Alexopoulos

Reputation: 1

In case the pickerview was alloc/inited and then added as a subview at the view then

[picker selectRow:selectedRow inComponent:0 animated:YES]; 

works only after

[self.view addSubview:picker];    

Upvotes: 0

Robin
Robin

Reputation: 10011

you need to store the value of the selected row in a variable call it selectedRow and when the view appears use this method on the object of pickerView

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{
selectedRow = row;
}

- (void)viewWillAppear
{
  [picker  selectRow:selectedRow inComponent:0 animated:YES];
}

to select the row in the particular component.

Upvotes: 2

KingofBliss
KingofBliss

Reputation: 15115

- (void) viewDidAppear:(BOOL)animated 
{
[pickerView selectRow:selectedItem inComponent:0 animated:YES]; 
}

Upvotes: 0

Mihir Mehta
Mihir Mehta

Reputation: 13843

call method

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated

After displaying it for second time

Upvotes: 0

Related Questions