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 vi开发者_开发技巧ewDidLoad 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.
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.)
Where you are initializing your pickerView? if you are doing with xib then it should be connected, i could find pickerView as IBOutlet in your code.
When you call numberOfComponentsInPickerView:
, it appears that you are calling it on an instance of UIViewController
and not an instance of ThirdViewController
. If necessary, cast the viewController to make sure the compiler knows it is an instance of the latter.
精彩评论