title: UIPickerView - populate 1 picker with multiple arrays
body: <p>I got a Problem with a Picker View in Xcode 4.4. I want to populate 1 Picker with multiple arrays. I watched / read a lot of tutorials and could get almost to work. This is my code:</p>
  
  <p>*.h</p>
  
  <pre><code> #import &lt;UIKit/UIKit.h&gt;
  
  @interface PickerView : UIViewController
  
  &lt;UIPickerViewDataSource,UIPickerViewDelegate&gt;
  {
      IBOutlet UIPickerView *picker;
  
      NSMutableArray *array_1;
      NSMutableArray *array_2;
  }
  
  @property (strong, nonatomic) IBOutlet UIView *pickerContainer;
  @property (strong, nonatomic) IBOutlet UITextField *textfield_1;
  @property (strong, nonatomic) IBOutlet UITextField *textfield_2;
  
  - (IBAction)showArray_1Picker:(id)sender;
  - (IBAction)showArray_2Picker:(id)sender;
  - (IBAction)hidePicker:(id)sender;
  
  @end
  </code></pre>
  
  <p>*.m</p>
  
  <pre><code>#import "PickerView.h"
  
  @interface PickerView ()
  
  @end
  
  @implementation PickerView
  @synthesize pickerContainer;
  @synthesize textfield_1;
  @synthesize textfield_2;
  
  int textFieldTouched = 0;
  
  - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker
  {
      return 1;
  }
  
  - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent (NSInteger)component
  {
      switch(textFieldTouched)
      {
          case 1:
              return [array_1 count];
              break;
          case 2:
              return [array_2 count];
              break;
          default:
              return 0;
      }
  }
  
  
  - (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component
  {
      switch(textFieldTouched)
      {
          case 1:
              return [array_1 objectAtIndex:row];
              break;
          case 2:
              return [array_2 objectAtIndex:row];
              break;
          default:
              return @"nothing";
      }
  }
  
  - (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
  {
      switch(textFieldTouched)
      {
          case 1:
              textfield_1.text = [array_1 objectAtIndex:[pickerView selectedRowInComponent:0]];
              break;
          case 2:
              textfield_2.text = [array_2 objectAtIndex:[pickerView selectedRowInComponent:0]];
              break;
      }
  }
  
   - (void)viewDidLoad
   {
      array_1 = [[NSMutableArray alloc] init];
      [array_1 addObject:@"1"];
      [array_1 addObject:@"2"];
      [array_1 addObject:@"3"];
      [array_1 addObject:@"4"];
      [array_1 addObject:@"5"];
  
      array_2 = [[NSMutableArray alloc] init];
      [array_2 addObject:@"a"];
      [array_2 addObject:@"b"];
      [array_2 addObject:@"c"];
      [array_2 addObject:@"d"];
      [array_2 addObject:@"e"];
  
      pickerContainer.frame = CGRectMake(0, 460, 320, 261);
  
      [super viewDidLoad];
  }
  
  - (void)viewDidUnload
  {
      [self setPickerContainer:nil];
      [self setTextfield_1:nil];
      [self setTextfield_2:nil];
      [super viewDidUnload];
  }
  
  - (IBAction)showArray_1Picker:(id)sender
  {
      [textfield_1 resignFirstResponder];
      textFieldTouched = 1;
      [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationDuration:0.3];
      pickerContainer.frame = CGRectMake(0, 200, 320, 261);
      [UIView commitAnimations];
  }
  
  - (IBAction)showArray_2Picker:(id)sender
  {
      [textfield_2 resignFirstResponder];
      textFieldTouched = 2;
      [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationDuration:0.3];
      pickerContainer.frame = CGRectMake(0, 200, 320, 261);
      [UIView commitAnimations];
  }
  
  - (IBAction)hidePicker:(id)sender
  {
      [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationDuration:0.3];
      pickerContainer.frame = CGRectMake(0, 460, 320, 261);
      [UIView commitAnimations];
  }
  
  @end
  </code></pre>
  
  <p>So i got 2 main problems:</p>
  
  <ol>
  <li><p>If i set textFieldTouched = 0 (or > 2) like i did before my picker is empty when it shows up. Anyway if I hide it again it writes the first value of the corresponding array in the textfield.</p></li>
  <li><p>If i set textFieldTouched = 1 or =2 the picker is populated with the related array no matter what text field I try to edit. But if I scroll the values on the picker out of my sight they have changed when i scroll back.</p></li>
  </ol>
  
  <p>I want to have it like this:</p>
  
  <p>If i touch text field 1 the picker shows up populated with array 1</p>
  
  <p>If i touch text field 2 the picker shows up populated with array 2</p>
  
  <p>ans so on.</p>
  
  <p>Can u help me with that?</p>
  
  <p>I've uploaded the code so you can look at it better.</p>
  
  <p><a href="https://dl.dropbox.com/u/5858884/ProblemCode.zip" rel="nofollow">https://dl.dropbox.com/u/5858884/ProblemCode.zip</a></p>
  
  <p>P.S.</p>
  
  <p>English is not my mother language so please excuse mistakes ;)</p>
  
  <p>I started programming 2 weeks ago, so if u find something wrong in my code that doesnt belong to my problem please tell me so I can get better :)</p>