0

My UIPicker is crashing if the NSArray of objects is greater than 3, with the following error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSAutoreleasePool pickerView:titleForRow:forComponent:]: unrecognized selector sent to instance

Here is my code for the functions:

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

  self.glassPickerOptions = [[NSArray alloc] initWithObjects:@"3mm",@"4mm",@"DG4+4",@"DG4+6",nil];
  [glassPicker setFrame:CGRectMake(0, 0, 320, 162)];
  [glassPicker selectRow:1 inComponent:0 animated:NO];
}

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSInteger glassPickerOptionsCount = self.glassPickerOptions.count;
    NSLog(@"%i", glassPickerOptionsCount);
    return glassPickerOptionsCount;
}

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

Hopefully I havent missed anything. Thanks in advance

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
Leon
  • 715
  • 1
  • 8
  • 22
  • Are your UIPickerViewDataSource and UIPickerViewDelegate outlets on your UIPickerView set to the class containing pickerView:titleForRow:forComponent:? If so, it seems that perhaps your class containing pickerView:titleForRow:forComponent: is being released (or wasn't ever retained and expired in an autorelease pool) – Jacob Jennings Sep 29 '11 at 21:41
  • This is my interface declaration `@interface PickerView : UIViewController `. This is being loaded as a subview from another ViewController. This ViewController contains the pickerView:titleForRow:forComponent: method. – Leon Sep 29 '11 at 22:20

1 Answers1

0

It seems that you overrelease your picker view, you can see this because the message is being sent to an autoreleasepool and not the object you expect, you should check out your retain/releases for your picker see whats going on, cant really tell from the code posted...

Daniel
  • 22,363
  • 9
  • 64
  • 71
  • Sorry, I'm new to iPhone programming, how can I check my retains/releases? – Leon Sep 29 '11 at 22:21
  • [Interface](http://pastie.org/2613757) [Implementation](http://pastie.org/2613771) – Leon Sep 29 '11 at 22:39
  • Where is your PickerView : UIViewController created/presented? in a navigation controller, tab controller? It's the PickerView : UIViewController itself that is getting overreleased. Also, a recommendation about naming conventions - PickerView is very close to UIPickerView, which is the actual spinner object's name. It's a UIViewController, and calling it PickerView sort of suggests that it's a UIView. I would recommend something more along the lines of GlassPickerViewController – Jacob Jennings Sep 30 '11 at 14:12
  • Thanks, I definitely agree with that naming convention. Here is the ViewController which calls the PickerView: [Interface](http://pastie.org/2617371) [Implementation](http://pastie.org/2617367) Thanks for your help! – Leon Sep 30 '11 at 14:55
  • Aha, sorted it. I added `@class GlassPickerViewController` and added then instantiated the glassPicker variable and set it to `(retain)`. Thanks all! – Leon Sep 30 '11 at 15:20