Search code examples
objective-ciosdatasourceuipickerview

2 different datasources


Im making an app and I want to fill two UITextView with two different UIPickerView.

My code


controller.h

UIPickerView *inputType;
UIPickerView *inputFormat;

controller.m

inputFormat=[[UIPickerView alloc] initWithFrame:CGRectZero];
inputType=[[UIPickerView alloc] initWithFrame:CGRectZero];
txtFormat.inputView=inputFormat;
txtType.inputView=inputType;

My problem comes now:

I want to fill the pickers with different data and I don't know how to do it.
I know how to set the datasource for one picker but not for two.

How should I do it?


Solution

  • Take a look at the delegate methods


    UIPickerViewDelegate

    – numberOfComponentsInPickerView:
    – pickerView:numberOfRowsInComponent:
    

    You can figure out which UIPickerView is asking for it's content by testing the pickerView variable. Just connect an outlet to both UIPickerViews. So you can select the same object as the data source of the two picker views.


    For example

    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
        if (pickerView == self.firstPickerView) {
            return 10;
        } else if (pickerView == self.secondPickerView) {
            return 15;
        }
    }