来自用户先前文本字段输入的UIPickerview过滤器列表

时间:2012-11-27 04:51:17

标签: iphone ios

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的用户输入加载UITextField的正确方向,以便他们输入上一个文本字段 apple < / strong>,我想在UIPickerView中显示红色,绿色,蓝色,或者如果他们输入橙色,我希望看到橙色,黄色。

2 个答案:

答案 0 :(得分:0)

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

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

现在你所需要的只是int(看起来你正在使用variabla)代表输入 - 例如,如果用户输入“Apple”,那么你就设置了{{ 1}}到0;如果他们输入“Orange”,则将variabla设置为1;等等。

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

variabla

您只需向- (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]; } 添加另一个NSArray即可添加对所需字数的支持。

答案 1 :(得分:0)

只需使用BOOL变量检查用户点击的按钮,苹果或橙色......

这里采用像

这样的变量
BOOL isApple;

当使用点击苹果按钮加载当时在方法设置tru中的pickerview这个变量就像波纹管那样..

-(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];
    }
}