How can I code multiple UIPickerViews in Xcode in a storyboard? (Objective-C)

367 views Asked by At

I'm trying to make an app that uses storyboarding and I want to put UIPickerViews on multiple pages. I can get one to work just fine but can't seem to figure out how to code a second one.

I did not add them programatically, I drag-dropped them onto the view. Here is my code that controls the UIPickerViews.

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView{
    if(thePickerView.tag == 1) {
        return 2;
    } else if(thePickerView.tag == 2) {
        return 2;
    } else {
        assert(NO);
    }
}

-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component{
    if(thePickerView.tag == 1){
        switch (component) {
            case 0:
                return flavourArray.count;
                break;
            case 1:
                return icingflavourArray.count;
                break;
            default:
                break;
        }
        return 0;
    }
    else if(thePickerView.tag == 2){
        switch (component) {
            case 0:
                return colorArray.count;
                break;
            case 1:
                return cheeseflavourArray.count;
                break;
            default:
                break;
        }
        return 0;
    }
    else
        return 0;
}


-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (thePickerView.tag == 1){
        switch (component) {
            case 0:
                return [flavourArray objectAtIndex:row];
                break;
            case 1:
                return [icingflavourArray objectAtIndex:row];
                break;
            default:
                break;
        }
        return 0;
    }
    else if (thePickerView.tag == 2){
        switch (component) {
            case 0:
                return [colorArray objectAtIndex:row];
                break;
            case 1:
                return [cheeseflavourArray objectAtIndex:row];
                break;
            default:
                break;
        }
        return 0;
    }
    else
        return 0;
}

In this code I tried using tags to differentiate between the UIPickerViews but I have also tried just using their names. Do my if statements make sense with what I'm trying to do or is there another way to program multiple UIPickerViews? Both of the UIPickerViews are synthesized so I don't understand why with this code the first one would work fine, with the correct arrays, but the second one appears blank.

If this topic is already somewhere on the site I apologize, I tried pretty extensively to find a solution but couldn't. Thanks for any help!

0

There are 0 answers