無法識別的選擇,我不能相信我跌倒在這麼簡單的東西:按下按鈕
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Tap me" forState:UIControlStateNormal];
[button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(50, 50, 120, 60);
[self.view addSubview:button];
}
return self;
}
-(void)test {
NSLog(@"Test");
}
它,當我按下按鈕,與unrecognized selector sent to instance
錯誤崩潰。
有人知道我會在這裏做錯嗎?
編輯 - 錯誤信息:
-[__NSCFString test]: unrecognized selector sent to instance 0x29ee30
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString test]: unrecognized selector sent to instance 0x29ee30'
編輯 - 它是如何呈現(ARC):
DemoViewController *demoVC = [[DemoViewController alloc] init];
[self.window addSubview:demoVC.view];
[self.window makeKeyAndVisible];
請問您能否發佈確切的錯誤代碼? – Pfitz
你在.h文件中聲明瞭'test'嗎? – WendiKidd
你確定你收到「無法識別的選擇器發送到實例」,而不是另一個異常? –