1

Can I use one UIPickerView with many UITextFields, or should I create many pickerviews, one for each UITextField (I have all the UITextFields in the same view)?

Code cracker
  • 3,105
  • 6
  • 37
  • 67
izan
  • 727
  • 2
  • 11
  • 22

4 Answers4

4
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField1.editing == YES)
    {
        textFieldName=textField1;
    }
    else
        if (textField2.editing == YES)
        {
            textFieldName=textField2;
        }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Prashant Kumar
  • 419
  • 1
  • 5
  • 15
3

Use the Array list view and then get the position.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Prashant Kumar
  • 419
  • 1
  • 5
  • 15
2

Please declare this textFieldName globally:

NSString * textFieldName;

Allocation in didLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [pickerView setHidden:YES];

    textFieldName=[NSString alloc]init];

    pickerArray1 = [[NSMutableArray alloc] initWithObjects:@"apple", @"mango", @"banana", nil];

    pickerArray2 = [[NSMutableArray alloc] initWithObjects:@"black", @"white", @"green", nil];
}

Please set text field name in textFieldName string:

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    [pickerView setHidden:YES];
    if (textField1.editing == YES)
    {
        textFieldName=textField1;
        [pickerView setHidden:NO];
    }
    else
        if (textField2.editing == YES)
        {
            textFieldName=textField2;
            [pickerView setHidden:NO];
        }
    }

Then use this textFieldName to check in the pickerview method:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
    if (textFieldName isEqualToString:@"textField1")
    {
        return [pickerArray1 count];
    }
    else
        if (textFieldName isEqualToString:@"textField2")
        {
            return [pickerArray2 count];
        }
    }

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    if (textFieldName isEqualToString:@"textField1")
    {
        return [pickerArray1 objectAtIndex:row];
    }
    else
        if (textFieldName isEqualToString:@"textField2")
        {
            return [pickerArray2 objectAtIndex:row];
        }
    }

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (textFieldName isEqualToString:@"textField1")
    {
        textField1.text= [pickerArray1 addObjectAtIndex:row];
    }
    else
        if (textFieldName isEqualToString:@"textField2")
        {
            textField2.text= [pickerArray2 addObjectAtIndex:row];
        }
        [pickerView setHidden:YES];
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jaspreet Singh
  • 1,180
  • 2
  • 12
  • 30
0

I think one Picker view with multiple textfields would be enough. You can easily tag the textfields and process them according to your needs.

Multiple sources for UIPickerView on textfield editing

http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/12952-tutorial-uipickerview-basics-basic-tip-calculator.html

Will help you get started

Community
  • 1
  • 1
visakh7
  • 26,380
  • 8
  • 55
  • 69