UIpickerview组件在不同的UIViews中,如何访问?

时间:2011-06-16 13:58:00

标签: iphone objective-c xcode uipickerview

如何从不同UIViews中的UIpickerview组件访问(获取用户选择)(不使用“界面”构建器)?

感谢。

这是我的代码:

UIView *myView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
UIView *myView2 = [[UIView alloc] initWithFrame:CGRectMake(20, 250, 100, 100)];

[myView1 setBackgroundColor:[UIColor blueColor]];
[myView2 setBackgroundColor:[UIColor redColor]];

[self.view addSubview:myView1];
[self.view addSubview:myView2];

UIPickerView *pickerView1 = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
pickerView1.delegate = self;
pickerView1.showsSelectionIndicator = YES;
[myView1 addSubview:pickerView1];

UIPickerView *pickerView2 = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
pickerView2.delegate = self;
pickerView2.showsSelectionIndicator = YES;
[myView2 addSubview:pickerView2];

我有这个回调,但是一旦我将pickerView放在多个UIViews中就失败了:

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(@"Selected Color: %@. Index of selected color: %i", [arrayColors objectAtIndex:row], row); 
} 

这是问题(但不知道解决方法):

[self.view addSubview:pickerView1]; //<-- this works (able to get a response)
//[myView1 addSubview:pickerView1];  //<-- this fails 

我不知道为什么我必须在主视图中使用它?

4 个答案:

答案 0 :(得分:1)

由于两个pickerviews都有相同的委托,因此您需要在委托方法中区分它们。一种方法是使用tag属性。

设置每个选择器视图的标记属性。然后在您的UIPickerView委托方法pickerView:didSelectRow:inComponent:中,查询标记并做出相应的响应。

#define kVIEW1_PICKERVIEW_TAG 123
#define kVIEW2_PICKERVIEW_TAG 456

UIView *myView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
UIView *myView2 = [[UIView alloc] initWithFrame:CGRectMake(20, 250, 100, 100)];

[myView1 setBackgroundColor:[UIColor blueColor]];
[myView2 setBackgroundColor:[UIColor redColor]];

[self.view addSubview:myView1];
[self.view addSubview:myView2];

UIPickerView *pickerView1 = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
pickerView1.delegate = self;
pickerView1.tag = kVIEW1_PICKERVIEW_TAG;
pickerView1.showsSelectionIndicator = YES;
[myView1 addSubview:pickerView1];

UIPickerView *pickerView2 = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
pickerView2.delegate = self;
pickerView2.tag = kVIEW2_PICKERVIEW_TAG;
pickerView2.showsSelectionIndicator = YES;
[myView2 addSubview:pickerView2];

-

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

switch( pickerView.tag ) {
    case kVIEW1_PICKERVIEW_TAG:
       /* code */
        break;
    case kVIEW2_PICKERVIEW_TAG:
        /* code */
        break;
    default:
        break;
    }

}

答案 1 :(得分:0)

@jdl您需要阅读UIPickerView委托方法。并且还委托协议。

答案 2 :(得分:0)

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 

 }

答案 3 :(得分:0)

您可以在UIPickerViewDelegate协议中使用委托方法 pickerView:didSelectRow:inComponent: 并使用标记来识别选择器

指定标签:

pickerView1.tag = 1;
pickerView2.tag = 2;

在你的委托方法中:

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

if(thePickerView.tag == 1 ){
//do this
}

else if (thePickerView.tag == 2){
//do this 
}

}