我认为这可能是一个非常基本的问题,但经过几个小时的搜索后,我可以找到一些人解决我的问题,但我找不到任何解决方案。
我刚刚开始使用iPhone开发,虽然我已经编写了Android和Blackberry应用程序,因此Objective-C有点像Java的转换。
首先,当我尝试使用UIPickerView打开屏幕时,我收到以下错误消息:
[UIViewController numberOfComponentsInPickerView:]:无法识别的选择器发送到实例0x4b4e0d0 2011-09-09 15:57:19.619 TabBar [4945:207] * 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [UIViewController numberOfComponentsInPickerView:]:无法识别的选择器发送到实例0x4b4e0d0 “
这是我的头文件:
#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
这是我的实施文件:
#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
我已经检查过,委托和dataSource都连接到文件的所有者。我错过了什么吗?
感谢。
答案 0 :(得分:2)
我怀疑在Interface Builder / xib文件中,您需要将UIViewController类更改为ThirdViewController类。在任何情况下,错误消息都告诉您正在将消息发送到UIViewController实例,而不是您创建的子类的实例。常见(但不是唯一)原因是IB / xib设置 - 但您也以编程方式进行错误。 (使用IB / xib很容易,即使你已经使用iOS一段时间了。)
答案 1 :(得分:2)
你在哪里初始化你的pickerView?如果你正在使用xib然后它应该被连接,我可以在你的代码中找到pickerView作为IBOutlet。
答案 2 :(得分:1)
当您致电numberOfComponentsInPickerView:
时,您似乎是在UIViewController
的实例上调用它,而不是ThirdViewController
的实例。如有必要,转换viewController以确保编译器知道它是后者的实例。