如何将2个UIpickerView添加到一个UIview中?

时间:2014-06-03 04:27:42

标签: ios objective-c uiview uipickerview

我有点困惑。我有一个主要UIView,并且我已经在主要版本中粘贴了2 UIViews。 在每个"孩子" - UIView我有一个UIPickerView。我的问题是我对1-st 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?

2 个答案:

答案 0 :(得分:1)

你需要设置选择器视图标签这样

#Updated

myPickerView.tag = 1;  //It's for first PickerView
directionTranslationPickerView.tag = 2;  //It's for second 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设置标记必须

答案 1 :(得分:0)

我同意上面的答案作为一个选项,但不排除将每个选择器放在自己的UIView子类中并制作一个视图的子视图。这样做的好处是它允许你保持代码分开,这对我来说至少更容易维护。不利的一面是,你最终会有很多代表来回。