title: One UIPickerView Two Data Sources/Views works once then crashes
body: <p>I am trying to display a single UIPickerView in my app and depending on what button is selected a UIPickView will popup and populate with the relevant data for the button selected. No sure if it matters but one UIPickerView has three columns and one has two. I can get both of them to work perfectly when the are separate but the minute that I nest them into if statements it breaks.</p>
  
  <p>On the third time when I select the button that fires the opposite UIPickerView it crashes and give me the error of NSRangeException and that it is trying to find the object at index 2 in an array [0...1]. My understanding is that the program thinks that there is an object in a spot in the array that doesn't exist. </p>
  
  <p>I have tried the [picker reloadAllComponents] method and have it called each time the button is selected but as stated above that only works once and then it quits. The strange thing is that if I select the button that I pressed for the second set of code it works perfectly. It is when I select a button that would make the UIPickerView have to change its view that it crashes. </p>
  
  <p>I am really new to iOS development and Objective C and hope that someone out there knows what I am doing wrong. I will post any code that you guys need and will post a couple samples just to understand what is going on. </p>
  
  <p>Here is one of the buttons methods that sets an int to 2 and also calls the method to show the picker. </p>
  
  <pre><code>-(IBAction)linePopupPicker{
  picker = 2;
  [self pickerShow];
  }
  </code></pre>
  
  <p>This code is the other method which is pretty much the same as the other (I am aware that I haven't implemented the UserDefaults into the second button yet)</p>
  
  <pre><code>-(IBAction)namePicker{
  field  = 1;
  picker = 1;
  NSLog(@"The value of field is:%i", field);
  NSUserDefaults *pickerViewSelectionDefaults = [NSUserDefaults standardUserDefaults];
  [tipPicker selectRow:[pickerViewSelectionDefaults integerForKey:@"pickerViewSelectionKey"] inComponent:0 animated:NO];
  [tipPicker selectRow:[pickerViewSelectionDefaults integerForKey:@"pickerViewNameColor"] inComponent:1 animated:NO];
  [tipPicker selectRow:[pickerViewSelectionDefaults integerForKey:@"pickerViewNameFontSize"] inComponent:2 animated:NO];
  
  [self pickerShow];
  }
  </code></pre>
  
  <p>This is the method that calls the UIPickerView. I have it so that it emerges onto the view rather than being static. I thought that this would be the best place to reload the components.</p>
  
  <pre><code>-(IBAction)pickerShow{
  
  [UIView beginAnimations:nil context:nil];
  [UIView setAnimationDuration:0.5];
  CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 240);
  pickerView.transform = transform;
  [self.view addSubview:pickerView];
  
  
  [tipPicker reloadAllComponents];
  
  
  [UIView commitAnimations];  
  }
  </code></pre>
  
  <p>Finally here is just a sample of the code that is used throughout the various methods that need to be called for the UIPickerView. </p>
  
  <pre><code>- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
  if (picker ==2) {
      switch (component)
      {
          case 0:
          {
              UILabel *lineColorLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 130, 44)] autorelease];
              lineColorLabel.backgroundColor = [UIColor clearColor];
              NSString *lineTempColor = [colorArray1 objectAtIndex:row]; 
              //NSLog(@"The String Color Name:%@", tempColor);
              UIColor *myColor1 = [UIColor performSelector:NSSelectorFromString(lineTempColor)];
              lineColorLabel.backgroundColor =  myColor1;
              return lineColorLabel;
          }
          case 1:
          {
              UILabel *alphaLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0,40, 44)] autorelease];
              alphaLabel.text = [alphaArray objectAtIndex:row];
              alphaLabel.font = [UIFont systemFontOfSize:18];
              alphaLabel.textAlignment = UITextAlignmentCenter;
              alphaLabel.backgroundColor = [UIColor clearColor];
              alphaLabel.shadowColor = [UIColor whiteColor];
              return alphaLabel;
          }
          default:
              break;
      }
  }
  
  else if (picker == 1) 
   {
      switch (component)
      {
          case 0:
          {
              UILabel *fontLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 130, 44)] autorelease];
              fontLabel.backgroundColor = [UIColor clearColor];
              fontLabel.shadowColor = [UIColor whiteColor];
              fontLabel.text = [fontNames objectAtIndex:row];
              NSString *font = [fontNames objectAtIndex:row];
              fontLabel.font = [UIFont fontWithName:font size:18.0];
              fontLabel.textAlignment = UITextAlignmentLeft;
              return fontLabel;
          }
          case 1:
          {
  
  
              UILabel *colorLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 35, 44)] autorelease];
              NSString *tempColor = [colorArray objectAtIndex:row]; 
              //NSLog(@"The String Color Name:%@", tempColor);
              UIColor *myColor = [UIColor performSelector:NSSelectorFromString(tempColor)];
              colorLabel.backgroundColor =  myColor;
  
  
              return colorLabel;
          }
          case 2:
          {
              UILabel *sizeLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0,40, 44)] autorelease];
              sizeLabel.text = [fontSizeArray objectAtIndex:row];
              sizeLabel.font = [UIFont systemFontOfSize:18];
              sizeLabel.textAlignment = UITextAlignmentCenter;
              sizeLabel.backgroundColor = [UIColor clearColor];
              sizeLabel.shadowColor = [UIColor whiteColor];
              return sizeLabel;
          }
          default:
              break;
      }
  
  }
  
      return 0;
  }
  </code></pre>
  
  <p>Here are the data methods:</p>
  
  <pre><code>- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { 
  if (picker == 2) {
      return 2;
  }else if (picker == 1) {
      return 3;
  }
  
  //return 3; 
  }
  
  - (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { // This method also needs to be used. This asks how many rows the UIPickerView will have.
  if (picker == 2) {
      if (component == 0) {
          return [colorArray1 count];
      }
      return [alphaArray count];
  }else if (picker == 1) {
      if (component == 0) {
          return [fontNames count];
      }else if (component == 1) {
          return [colorArray count];
      }
      return [fontSizeArray count];
  }
  
  }
  </code></pre>
  
  <p>I hope this post provides all of the information that is need to help me get this fixed. I really appreciate it and hope that I am just missing something really simple. Thank you in advanced.</p>
  
  <p>I am really new to iOS development and Objective C so this problem is really stumping me. </p>