0

I'm building an app with two UI textFields. When the UI textfields are touched a UI Picker comes up for the user to select a value to stay in the UI textField. When I test the app and touch one UI TextView and scroll the UI Picker, both UI textFields change values. Meaning, each UI Text Field is being affected from each UI Picker. Anyone know why? Thank you.

@interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate>

@end

@implementation ViewController
@synthesize ageTextField;
@synthesize relationshipTextField;
@synthesize resultLabel;
@synthesize calculateButton;
@synthesize agePickerView;
@synthesize relationshipPickerView;
@synthesize ageArray;
@synthesize relationshipArray;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //age pickerView

    self.agePickerView = [[UIPickerView alloc]init];

    self.agePickerView.delegate = self;
    self.agePickerView.dataSource = self;
    self.agePickerView.showsSelectionIndicator = YES;
    self.ageTextField.inputView = self.agePickerView;

    self.ageArray = @[ @"1", @"2", @"3", @"4", @"5", @"5", @"6", @"7", @"8", @"9", @"10",       @"11", @"12"
 , @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20", @"21", @"22", @"23", @"24",     @"25", @"26", @"27", @"28", @"29", @"30", @"31", @"32", @"33", @"34", @"35", @"36", @"37", @"38", @"39", @"40", @"41", @"42", @"43", @"44", @"45", @"46", @"47", @"48", @"49", @"50", @"51", @"52", @"53", @"54", @"55", @"56", @"57", @"58", @"59", @"60", @"61", @"62", @"63", @"64", @"65", @"66", @"67", @"68", @"69", @"70", @"71", @"72", @"73", @"74", @"75", @"76", @"77", @"78", @"79", @"80", @"81", @"82", @"83", @"84", @"85", @"86", @"87", @"88", @"89", @"90", @"91", @"92", @"93", @"94", @"95", @"96", @"97", @"98", @"99", @"100" ];
[self pickerView:self.agePickerView
didSelectRow:0
inComponent:0];

//relationship pickerView

self.relationshipPickerView = [[UIPickerView alloc]init];

self.relationshipPickerView.delegate = self;
self.relationshipPickerView.dataSource = self;
self.relationshipPickerView.showsSelectionIndicator = YES;
self.relationshipTextField.inputView = self.relationshipPickerView;

self.relationshipArray = @[ @"1", @"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13", @"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25", @"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37", @"38",@"39",@"40",@"41",@"42",@"43",@"44",@"45",@"46",@"47",@"48",@"49",@"50", @"51", @"52", @"53", @"54", @"55", @"56", @"57", @"58", @"59", @"60", @"61", @"62", @"63", @"64", @"65", @"66", @"67", @"68", @"69", @"70", @"71", @"72", @"73", @"74", @"75", @"76", @"77", @"78", @"79", @"80", @"81", @"82", @"83", @"84", @"85", @"86", @"87", @"88", @"89", @"90", @"91", @"92", @"93", @"94", @"95", @"96", @"97", @"98", @"99", @"100"];

[self pickerView:self.relationshipPickerView
    didSelectRow:0
     inComponent:0];

}

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

-(int) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component   {
    return [self.ageArray count];
    return [self.relationshipArray count];
}

-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row  forComponent:(NSInteger)component{
    return [self.ageArray objectAtIndex:row];
    return [self.relationshipArray objectAtIndex:row];
}

-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:   (NSInteger)component{
    self.ageTextField.text = [self.ageArray objectAtIndex:row];
    self.relationshipTextField.text = [self.relationshipArray objectAtIndex:row];
}

- (void)viewDidUnload
{
    [self setAgeTextField:nil];
    [self setRelationshipTextField:nil];
    [self setResultLabel:nil];
    [self setCalculateButton:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

 @end
user1681673
  • 368
  • 1
  • 6
  • 28

1 Answers1

3
-(int) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component   {
    return [self.ageArray count];
    return [self.relationshipArray count];
}

That's not how C (or Objective-C) works. It will always only execute the first line.

You need to conditionalize the return value based on which UIPickerView is asking for the information:

if (pickerView == self.agePickerView) {
  return [self.ageArray count];
} else {
  return [self.relationshipArray count];
}

And then apply that same pattern to all the datasource methods.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • It worked. I'll mark it as correct when I can. Thanks! Also, how do dismiss a UI Picker once a value has been selected? – user1681673 Dec 18 '12 at 04:01
  • @user1681673 since you're using the `UIPickerView` as the `inputView` of a `UITextField`, you would simply tell the textField to `resignFirstResponder`. – Dave DeLong Dec 18 '12 at 04:16
  • I created a new thread about dismissing the picker view. Can you check it out? Thanks. http://stackoverflow.com/questions/13926830/how-to-dismiss-a-ui-picker-that-is-connected-to-a-ui-text-field – user1681673 Dec 18 '12 at 06:04