IOS中具有单个选取器视图的多个文本字段

时间:2016-05-06 05:31:51

标签: ios tags uipickerview

我是IOS新手我想创建多个文本字段,只有一个选择器。假设我需要五个文本字段意味着如果我点击第一个文本字段选择器视图可以加载第一个数组然后我去第二个文本字段意味着选择器视图可以自动加载第二个数组并继续到最后一个字段和数组。请给我基于我的想法和建议问题。

2 个答案:

答案 0 :(得分:1)

按照以下步骤实现目标。

  1. 从以下链接下载通用类:

    LabeledPickerView.h

    LabeledPickerView.m

  2. 将此类复制到项目中,并将“LabeledPickerView.h”导入ViewController.h文件。此外,添加 UIPickerViewDataSource UIPickerDelegate UITextFieldDelegate

  3. 现在,使用以下方法初始化Picker: -

    -(LabeledPickerView *)GetPickerViewWithTag:(int)Tag {
    
       LabeledPickerView *pickerView = [[LabeledPickerView alloc] init];
    
       pickerView.dataSource = self;
    
       pickerView.delegate = self;
    
       pickerView.tag = Tag;
    
       pickerView.backgroundColor = [UIColor whiteColor];
    
       pickerView.showsSelectionIndicator = YES;
    
       return pickerView;
    
    }
    

    此方法将返回PickerView的属性。

  4. 现在,我们将使用UIPickerView委托和数据源方法将数据添加到此处: -

    //Based on the text fields tags, you can populate the data in PickerView.
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
         return 1;     //Returns components in PickerView. Change with switch - case statement if you want more components in any of the text fields.
    }
    
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {     
    
        int tag = (int)((UIPickerView *)pickerView).tag;
        switch (tag) {
              case 10: {
                 if (YourArray.count > 0) {
                     return [YourArray count];
                 }
                 else {
                     return 0;
                 }
                 break;
              }
              default:
              break;
        }
        return 0;
    }
    
    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
        int tag = (int)((UIPickerView *)pickerView).tag;
        switch (tag) {
              case 10: {
                if ([YourArray count] > 0) {
                    return [YourArray objectAtIndex:row];
                }
                else {
                    return @"";
                }
                break;
             }
             default:
             break;
         }
         return @"";
     }
    
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
          int tag = (int)((UIPickerView *)pickerView).tag;
          switch (tag) {
                case 10: {
                    if ([YourArray count] > 0) {
                      YourTextField.text = [YourArray objectAtIndex:row];
                }
                else {
                      txtCity.text = @"";
                }
                break;
          }
          default:
          break;
       }
     }
    
  5. 现在,开始编辑时打开选择器如下:

    -(void)textFieldDidBeginEditing:(UITextField *)textField {
        if (textField == firstTextField) {
            LabeledPickerView *picker = [self GetPickerViewWithTag:10];  //Change tag as text field changes..
            textField.inputView = picker;  
            [picker selectRow:0 inComponent:0 animated:YES];
            [self pickerView:picker didSelectRow:0 inComponent:0];
         }
    }
    
  6. 在此方法中添加您拥有的文本字段。不要忘记为每个文本字段分配不同的标记。

    通过使用上面的代码,您可以使用具有多个文本字段的单个选择器视图。

    快乐编码.. !!

答案 1 :(得分:0)

可能会对你有帮助,

我在我的项目中完成了相同的代码并且工作正常。在这里,我给你三个文本域的代码。

    NSMutableArray *pickerTitleData, *pickerCityData, *pickerStateData;
@property (strong, nonatomic) IBOutlet UItextfield *txtTitle;
@property (strong, nonatomic) IBOutlet UItextfield *txtTitle;
@property (strong, nonatomic) IBOutlet UItextfield *txtTitle;
    UIPickerView *TitleSelect,*CitySelect, *StateSelect;




for address type
        pickerTitleData =[[NSMutableArray alloc]initWithObjects:@"Home",@"Office",@"Other", nil];

        TitleSelect = [[UIPickerView alloc]init];
        TitleSelect.dataSource = self;
        TitleSelect.delegate = self;
        TitleSelect.showsSelectionIndicator = YES;
        txtTitle.inputView = TitleSelect;

// same you have three other array


#pragma mark - Picker View Data source for city

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    @try
    {
        if (pickerView == TitleSelect)
        {
            return pickerTitleData.count;
        }

        else if (pickerView == CitySelect)
        {
            return pickerCityData.count;
        }

        else if (pickerView == StateSelect)
        {
            return pickerStateData.count;
        }

        return 1;
    }
    @catch (NSException *exception)
    {
        NSLog(@"exception--%@",exception.description);
    }
}

#pragma mark- Picker View Delegate for city

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    @try
    {
        if (pickerView == TitleSelect)
        {
            [txtTitle setText:[pickerTitleData objectAtIndex:row]];
        }

        else if (pickerView == CitySelect)
        {
            [txtTownOrCity setText:[pickerCityData objectAtIndex:row]];
        }

        else if (pickerView == StateSelect)
        {
            [txtState setText:[pickerStateData objectAtIndex:row]];
        }
    }
    @catch (NSException *exception)
    {
        NSLog(@"%@",exception.description);
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    @try
    {
        if (pickerView == TitleSelect)
        {
            return pickerTitleData[row];
        }

        else if (pickerView == CitySelect)
        {
            return pickerCityData[row];
        }

        else if (pickerView == StateSelect)
        {
            return pickerStateData[row];
        }

        return @"";

    }
    @catch (NSException *exception)
    {
        NSLog(@"%@",exception.description);
    }
}