我现在有 2 个 UIPickerView,我想稍后再添加三个。代码如下:
-(void)viewDidLoad{
Pos_x = 5;
Pos_y = 70;
for (i= 0; i<2; i++) {
myPickerView = [[UIPickerView alloc] init];
myPickerView.frame = CGRectMake(Pos_x,Pos_y,150,300);
myPickerView.tag = i;
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];
if (i==0) {
// Init the data array.
Array_1 = [[NSMutableArray alloc] init];
// Add some data for demo purposes.
[Array_1 addObject:@"One"];
[Array_1 addObject:@"Two"];
[Array_1 addObject:@"Three"];
[Array_1 addObject:@"Four"];
[Array_1 addObject:@"Five"];
}
if (i==1) {
Array_2 = [[NSMutableArray alloc] init];
// Add some data for demo purposes.
[Array_2 addObject:@"One1"];
[Array_2 addObject:@"Two1"];
[Array_2 addObject:@"Three1"];
[Array_2 addObject:@"Four1"];
[Array_2 addObject:@"Five1"];
}
Pos_x = Pos_x + 162;
}
}
我已经成功创建了两个带有标签的 UIPickerView。现在的问题是在 UIPickerView 中添加数据。
我正在这样做
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//return [Array_1 objectAtIndex: row]; // If give only this line both UIPickerView show same value.
//How to show both
//return [Array_2 objectAtIndex: row];
// I also tried this code
if(pickerView == myPickerView){
if (i==0){
}
return [Array_1 objectAtIndex: row];
}
if (i==1){
}
return [Array_2 objectAtIndex: row];
}
}
现在我的问题是如何在两个 UIPickerView 上显示不同的不同数据?就像在 pickerview.tag ==0 上一样,它会显示 return [Array_1 objectAtIndex: row]; 并且 pickerview.tag ==1 它会显示 return [Array_2 objectAtIndex: row];?欢迎任何想法或建议。