gpt4 book ai didi

iphone - 按下按钮时无法识别的选择器

转载 作者:可可西里 更新时间:2023-11-01 05:06:30 24 4
gpt4 key购买 nike

我不敢相信我在这么简单的事情上绊倒了:

- (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];

最佳答案

如果你使用的是ARC,那么demoVC.view会在函数结束后立即realeasd,而不是像这样初始化

DemoViewController *demoVC = [[DemoViewController alloc] init];

围绕 demoVC 建立一个强大的属性并将其初始化为

self.demoVC = [[DemoViewController alloc] init];

关于iphone - 按下按钮时无法识别的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11284927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com