gpt4 book ai didi

objective-c - UIButton 调用选择器会使应用程序崩溃

转载 作者:行者123 更新时间:2023-12-03 16:51:26 25 4
gpt4 key购买 nike

在我的应用程序中,我有一个按钮,它是已在 .h 文件中声明的实例变量。现在,在为其分配目标并实际按下按钮后,应用程序崩溃,在调试器中给出以下输出:

-[__NSCFString handleActionSheet:]: unrecognized selector sent to instance 0x9090330
2012-05-04 17:51:02.646 financeAppV2[2595:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString handleActionSheet:]: unrecognized selector sent to instance 0x9090330'
*** First throw call stack:
(0x13d1022 0x1562cd6 0x13d2cbd 0x1337ed0 0x1337cb2 0x13d2e99 0x1e14e 0x1e0e6 0xc4ade 0xc4fa7 0xc3d8a 0x2dfa1a 0x13a599e 0x133c640 0x13084c6 0x1307d84 0x1307c9b 0x12ba7d8 0x12ba88a 0x1b626 0x29dd 0x2945)
terminate called throwing an exceptionCurrent language: auto; currently objective-c

我的代码如下所示:

saveButton=[UIButton buttonWithType:UIButtonTypeCustom];
[saveButton setBackgroundImage:[UIImage imageNamed:@"intervalbutton"] forState:UIControlStateNormal];
[saveButton setBackgroundImage:[UIImage imageNamed:@"intervalbuttonpressed"] forState:UIControlStateHighlighted];
saveButton.alpha=.8;
saveButton.frame= frame;
UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(0, 2, saveButton.frame.size.width, 21)];
[saveButton addSubview:label];

[saveButton addTarget:self action:@selector(handleActionSheet:) forControlEvents:UIControlEventTouchDown];

[self.view addSubview:saveButton];


-(void)handleActionSheet:(id)sender{
NSLog(@"working");

}

知道应用程序可能崩溃的原因吗?

最佳答案

如果没有看到代码的其余部分,很难具体说明,但 self 没有被正确保留。

您正确设置了按钮(据我所知)并将目标指向self。不幸的是,当您按下按钮时,self 已经超出了范围,并且 NSString 使用了相同的内存。当您发送字符串 handleActionSheet: 消息时,iOS 会感到惊讶。

关于objective-c - UIButton 调用选择器会使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10452197/

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