Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIPickerView ios problems - unrecognized selector

I think this is probably a very rudimentary question, but after a few hours of searching, I can find some people with my issue, but none of the solutions I found work.

I am just getting my feet wet with iPhone development, though I have writing Android and Blackberry apps, so the Objective-C is a bit of a switch from Java.

First of all, when I try to open the screen with a UIPickerView on it, I get this error message:

[UIViewController numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x4b4e0d0 2011-09-09 15:57:19.619 TabBar[4945:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x4b4e0d0'

Here is my header file:

#import <UIKit/UIKit.h>

@interface ThirdViewController : UIViewController <UIPickerViewDelegate,        UIPickerViewDataSource>{
UILabel *setUnits;
UIPickerView *pickerView;
NSMutableArray *pickItems;
}
@property (retain, nonatomic) UIPickerView *pickerView;
@property (retain, nonatomic) NSMutableArray *pickItems;

@end

And here is my implementation file:

#import "ThirdViewController.h"

@implementation ThirdViewController

@synthesize pickerView;
@synthesize pickItems;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

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

    pickItems = [[NSMutableArray alloc] init];
    [pickItems addObject:@"Pounds"];
    [pickItems addObject:@"Kilograms"];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

    return 1; //give components here
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:    (NSInteger)component {

     return [pickItems count];   //give rows here
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {   
    return [pickItems objectAtIndex:row];  // give titles 
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    //Do the step here
}

- (void)dealloc {
     [super dealloc];
}
@end

I have checked, and both the delegate and dataSource are connected to the file's Owner. Am I missing something?

Thanks.

like image 780
coder Avatar asked Mar 04 '26 09:03

coder


1 Answers

I suspect that in the Interface Builder / xib file you need to change your UIViewController class to the ThirdViewController class. In any case the error message is telling you that your are sending the message to a UIViewController instance and not an instance of the subclass you have created. Common (but not the only) cause is IB / xib settings - but you make the error programmatically too. (With IB / xib it is easy to do, even if you've been using iOS for a while.)

like image 88
Obliquely Avatar answered Mar 05 '26 23:03

Obliquely