我不敢相信我偶然发现了这么简单的事情:
- (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];