user3701608
user3701608

Reputation: 11

How to add 2 UIpickerViews into one UIview?

I am a little bit confused. I have one main UIView and I've pasted 2 UIViews in the main one. In each "child"-UIView I have a UIPickerView. My problem is that I have the following functions for the 1-st UIPickerView, but don't know how to do such for second one. Can anybody help me with that?

-(NSInteger)numberOfComponentsInPickerView:(UIViewController *)pickerView{
    return 1;
}


-(NSInteger)pickerView:(UIViewController *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if(component==option)
        return[options count];
    return 0;

}


-(NSString *)pickerView:(UIViewController *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(component==option)
         return[options objectAtIndex:row];
    return 0;
}


-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _optionLabel.text=[options objectAtIndex:[myPickerView selectedRowInComponent:0]];
}

So, are there any ways to do copies of these functions to my 2nd pickerView?

Upvotes: 0

Views: 919

Answers (2)

Jogendra.Com
Jogendra.Com

Reputation: 6454

You Need to set picker view tag Like this

#Updated

myPickerView.tag = 1;  //It's for first PickerView
directionTranslationPickerView.tag = 2;  //It's for second PickerView

Please Implement this two line where you create your pickerview .

    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    if (pickerView.tag==1) {
        return 1; //It's for first PickerView
    }else{
        return 1; // It's for second pickerview
    }

}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (pickerView.tag==1) {
        if(component==option)
            return[options count]; //It's for first PickerView
        return 0;

    }else{

        // It's for second pickerview
        return 0;

    }

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView.view.tag==1) {
        if(component==option)
            return[options objectAtIndex:row]; //It's for first PickerView
        return 0;
    }else{
        // It's for second pickerview
    }

}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView.view.tag==1) {
        _optionLabel.text=[options objectAtIndex:[myPickerView selectedRowInComponent:0]]; //It's for first PickerView

    }else{
        // It's for second pickerview
    }
}

Try this may be helpfull for you. Note : You need to set tag must for your UIPickerView

Upvotes: 1

RegularExpression
RegularExpression

Reputation: 3541

I agree with the answer above as an option, but wouldn't rule out putting each picker in its own UIView subclass and making those subviews of the one view. The advantage to this is that it allows you to keep the code separate which for me, at least, is easier to maintain. The downside is that you can end up with a lot of delegates back-and-forth.

Upvotes: 0

Related Questions