Search code examples
iphoneobjective-cioscocoa-touchuipickerview

How to use UIPickerView more efficiently?


I am using UIPickerView in one of my view, in the same view, I have to use the UIPickerView in more than 5 places. What I did was, created a outlet for UIPickerView and reused the same outlet in all location... for eg...

audio_cap_picker = nil;
// Creat picker view.
if (audio_cap_picker == nil) {
    audio_cap_picker = [[UIPickerView alloc] init];
    audio_cap_picker.delegate = self;
    audio_cap_picker.showsSelectionIndicator = YES;
    [self.view addSubview:audio_cap_picker];
}

Like above I have been creating the picker view when a button is clicked and its row title, and related information are set based on which button is clicked, It is working fine. However, the problem is when I am clicking a button because I am creating new every time, it is not highlighting the current selected row instead it is showing the first row as highlighted, so I was forced to use separate outlet for each UIPickerView.

Is there any way I can avoid creating different outlets for UIPickerView. Or is it the only way? which one is more efficient?

thanks.


Solution

  • When the user selects the 5th row for example , you store a value, right? When you show the picker the next time , you can move it's selection like this:

    int index = [valuessForFirstPicker indexOfObject:storeValueForFirstPicker];
    [picker selectRow:index inComponent:0 animated:TRUE];
    

    Hoep this helps.

    Cheers!