gpt4 book ai didi

iphone - 如何创建五个带标签的 UIPickerView?以及如何在 iPhone 中为每个 UIPickerView 赋予不同的名称?

转载 作者:行者123 更新时间:2023-11-29 11:02:59 29 4
gpt4 key购买 nike

我现在有2个UIPickerView,以后想再添加3个,代码如下:

-(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];?欢迎任何想法或建议。

最佳答案

请检查此代码:

-(void)viewDidLoad{
myPickerView = [[UIPickerView alloc] init];
myPickerView.frame = CGRectMake(5,70,150,300);
//myPickerView.tag = i;
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];

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"];
[Array_1 addObject:@"Six"];


myPickerView_2nd = [[UIPickerView alloc] init];
myPickerView_2nd.frame = CGRectMake(167,70,150,300);
//myPickerView_2nd.tag = i;
myPickerView_2nd.delegate = self;
myPickerView_2nd.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView_2nd];

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"];
[Array_2 addObject:@"Six1"];

}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
if([pickerView isEqual: myPickerView]){
// return the appropriate number of components, for instance
return 1;
}

if([pickerView isEqual: myPickerView_2nd]){
// return the appropriate number of components, for instance
return 1;
}
else
return 0;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {


if ( pickerView == myPickerView) // this is otherPickerview
{
return [Array_1 count];


}
if ( pickerView == myPickerView_2nd){
return [Array_2 count];


}
else{
NSUInteger numRows = 5;
return numRows;}

}

希望这对你有帮助...

关于iphone - 如何创建五个带标签的 UIPickerView?以及如何在 iPhone 中为每个 UIPickerView 赋予不同的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14975808/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com