0
int variabla;

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

return 1;
}

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


if (variabla == 1)
    return list4.count;

else if(variabla == 2)
    return list3.count;

if (variabla == 3)
    return list1.count;

if (variabla == 4)
    return list2.count;

return YES;

[pickerView reloadAllComponents];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (variabla == 1) {
    subjectField.text = [list4 objectAtIndex:row];
}
else if  (variabla == 2) {
    gradeField.text = [list3 objectAtIndex:row];
}
else if (variabla == 3) {
    mathField.text = [list1 objectAtIndex:[pickerView selectedRowInComponent:0]];
    selectedRow = [pickerView selectedRowInComponent:0];
    coreTView.text = [mathMeaning objectAtIndex:selectedRow];
}
if (variabla == 4) {
    elaField.text = [list2 objectAtIndex:[pickerView selectedRowInComponent:0]];
    selectedRow = [pickerView selectedRowInComponent:0];
    coreTView.text = [elaMeaning objectAtIndex:selectedRow];
}


[pickerView reloadAllComponents];
NSLog(@"row selected is %d", selectedRow);

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component  {

if (variabla == 1)
    return [list4 objectAtIndex:row];


if (variabla == 2)
    return [list3 objectAtIndex:row];

if (variabla == 3)
    return [list1 objectAtIndex:row];

if (variabla == 4)
    return [list2 objectAtIndex:row];

return NO;

[pickerView reloadAllComponents];
}

我想知道或指出如何根据以前的UIPickerView用户输入加载a橙色,我想看橙色,黄色。UITextFieldUIPickerView

4

2 回答 2

0

听起来是使用多维数组的好时机:

NSArray *titles = [[NSArray alloc] initWithObjects:
 [[NSArray alloc] initWithObjects:@"Red",@"Green",@"Blue",nil],
 [[NSArray alloc] initWithObjects:@"Orange",@"Yellow",@"Grape",nil],
 nil];

现在您只需要一个int(看起来您正在使用variabla)代表输入 - 例如,如果用户输入“Apple”,那么您将设置variabla为 0;如果他们输入“橙色”,则设置variabla为 1;等等。

然后,您可以相应地设置相关功能:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [[titles objectAtIndex:variabla] count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component  {
    return [[titles objectAtIndex:variabla] objectAtIndex:row];
}

您可以通过简单地将另一个添加NSArraytitles.

于 2012-11-27T05:07:03.690 回答
0

只需使用BOOL变量来检查用户单击哪个按钮,苹果或橙色...

这里取变量

BOOL isApple;

当使用点击苹果按钮加载pickerview时,在方法中设置tru这个变量,如下所示..

-(IBAction)btnApple_Clicked(id)sender{
   isApple =TRUE;
   [pickerView reloadAllComponents];
}

-(IBAction)btnOrange_Clicked(id)sender{
   isApple =FALSE;
   [pickerView reloadAllComponents];
}

然后在这个委托方法中像这样使用......

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
   if(isApple){
       return [appleArray count];
   }
   else{
       return [orangeArray count];
   }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component  {
    if (isApple) {
        return [appleArray objectAtIndex:row];
    }
    else{
        return [appleArray objectAtIndex:row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (isApple) {
        yourTextField.text = [appleArray objectAtIndex:row];
    }
    else{
        yourTextField.text = [appleArray objectAtIndex:row];
    }
}
于 2012-11-27T05:10:21.847 回答