我有点困惑。我有一个主要的UIView,并且已经在主要内容中粘贴了2个UIViews
在每个“孩子”-UIView中,我都有一个UIPickerView。我的问题是我对第一个UIPickerView具有以下功能,但对于第二个不知道如何执行此功能。有人可以帮我吗?

-(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]];
}


那么,有什么方法可以将这些功能复制到我的第二个pickerView中?

最佳答案

您需要设置选择器视图标签

#Updated

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


请在创建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
    }
}


试试这个可能对您有帮助。
注意:您需要为UIPickerView设置标签必须

关于ios - 如何将2个UIpickerViews添加到一个UIview中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24007134/

10-09 06:01