简体   繁体   中英

Multiple textfield with single picker view in IOS

I am new to IOS i want to create multiple textfield with only one picker progamatically. Suppose i need five textfield means if i clicked first textfield picker view can load the first array then i go to second textfield means picker view can load second array automatically and goes on upto last field and array.please give me idea and suggestion based on my problem.

Follow this steps to achieve your goal.

  1. Download below generic classes from given link:

    LabeledPickerView.h

    LabeledPickerView.m

  2. Copy this class into your project and import "LabeledPickerView.h" into your ViewController.h file. Also, Add UIPickerViewDataSource , UIPickerDelegate and UITextFieldDelegate .

  3. Now, initialized Picker with following method:-

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

    This method will return PickerView's properties.

  4. Now, We'll add data into this using UIPickerView Delegate and Data Source Methods as follow:-

     //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. Now, Open Picker When You Start Editing as follow:

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

In this method add as many text fields you have. Do't forget to assign different tags to each text field.

By Using above code, You can use single picker view with multiple text fields.

Happy Coding..!!

May be this will help you,

i done the same code in my project and it working. here i give you code for three textfields.

    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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM