UIPickerView ios 问题 - 无法识别的选择器
我认为这可能是一个非常基本的问题,但经过几个小时的搜索,我可以找到一些人解决我的问题,但我找到的解决方案都不起作用。
尽管我已经编写了 Android 和 Blackberry 应用程序,但我才刚接触 iPhone 开发,因此 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
这是我的实现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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑在 Interface Builder / xib 文件中,您需要将 UIViewController 类更改为 ThirdViewController 类。在任何情况下,错误消息都会告诉您正在将消息发送到 UIViewController 实例,而不是您创建的子类的实例。常见(但不是唯一)原因是 IB / xib 设置 - 但您也以编程方式犯了错误。 (使用 IB / xib 很容易做到,即使您已经使用 iOS 一段时间了。)
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.)
你在哪里初始化你的pickerView?如果你正在使用xib,那么它应该被连接,我可以在你的代码中找到pickerView作为IBOutlet。
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.
当您调用
numberOfComponentsInPickerView:
时,您似乎是在UIViewController
的实例上调用它,而不是在ThirdViewController
的实例上调用它。如有必要,强制转换 viewController 以确保编译器知道它是后者的实例。When you call
numberOfComponentsInPickerView:
, it appears that you are calling it on an instance ofUIViewController
and not an instance ofThirdViewController
. If necessary, cast the viewController to make sure the compiler knows it is an instance of the latter.