Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将2个UIpickerViews添加到一个UIview中?_Ios_Objective C_Uiview_Uipickerview - Fatal编程技术网

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

Ios 如何将2个UIpickerViews添加到一个UIview中?,ios,objective-c,uiview,uipickerview,Ios,Objective C,Uiview,Uipickerview,我有点困惑。我有一个主ui视图,我在主视图中粘贴了2个ui视图。 在每个“child”-UIView中,我都有一个UIPickerView。我的问题是,我为第一个UIPickerView提供了以下函数,但不知道如何为第二个UIPickerView提供这些函数。有人能帮我吗 -(NSInteger)numberOfComponentsInPickerView:(UIViewController *)pickerView{ return 1; } -(NSInteger)pickerVi

我有点困惑。我有一个主
ui视图
,我在主视图中粘贴了2个
ui视图
。 在每个“child”-
UIView
中,我都有一个
UIPickerView
。我的问题是,我为第一个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中?

您需要像这样设置picker view标记

#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设置tag must,我同意上面的答案,但不排除将每个选择器放在其自己的UIView子类中,并使这些子视图成为一个视图。这样做的好处是,它允许您将代码分开,至少对我来说,这样更容易维护。不利的一面是,你最终可能会遇到很多来回的代理

使用每个方法的
pickerView
参数确定正在处理哪个选择器视图。为每个返回正确的数据。您不需要2个UIPickerView,而是需要2个datasource,根据需要在datasource之间切换。谢谢,但是如果我的picker视图是“myPickerView”和directionTranslationPickerView,我必须如何以及在何处编写PickerWebg.tag=1和PickerWebj.tag=2。我的意思是如何连接它们?我已经更新了我的答案。您需要在创建PickerView的位置设置标记。试试这个。。谢谢:)那不行。它说:未知类型名“myPickerView”;你是说“UIPickerView”吗?“那么,有什么想法吗?”当然。IBOutlet UIPickerView*myPickerView;IBOutlet UIPickerView*directionTranslationPickerView;我已经在ViewDidLoad中创建了这些标记,没有任何错误,但现在我无法运行模拟器,因为出现了“Thread1:signal…”请在ViewDidLoad中设置这两个pickerview标记。