Omer Waqas Khan
Omer Waqas Khan

Reputation: 2413

setting index of UIPickerView iPhone

I have multiple view controllers in my application with uipickerview (which get visible on clicking a button), in all view controllers I have an array which is the same for all views. That array is also used in picker view.

Array: (34DL3611, 34GF0512, 34HH1734, 34HH1736, 34YCJ15)

The scenario is that if user is in ViewController1 and he selects some value like 34HH1736 (at index-3) from picker view and goes to ViewController2, then in that view the value 34HH1736 will be shown in a label.

Now user clicks on the button and uipickerview comes up, now the row value of picker view should be set at index-3 which is 34HH1736. But in my picker view it shows the first row selected which is index-0.

How can I achieve this?

Upvotes: 0

Views: 3021

Answers (3)

user2498295
user2498295

Reputation: 1

Put this in viewwillapear (it will only work when view will appear).

[pickerViewObj selectRow:selectRow inComponent:1 animated:YES]

Upvotes: 0

geminiCoder
geminiCoder

Reputation: 2906

I take it your using

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

And the method is being called?

Upvotes: 0

Yuvaraj.M
Yuvaraj.M

Reputation: 9811

Mr.illoGicalError is simple to do. UIPickerView have this property to set the selected index path. Please find this property below,

[pickerViewObj selectRow:selectRow inComponent:1 animated:YES];

selectRow is an int datatype. Please set selectRow = 3; When you comeback to UIPickerView it shows the third row is selected. You should store the selectedIndex path from the picker, then only you can do the same above. Please try it. Happy coding. Thanks.

Upvotes: 3

Related Questions