按下按钮时出现错误 EXC_BAD_ACCESS
Posted
技术标签:
【中文标题】按下按钮时出现错误 EXC_BAD_ACCESS【英文标题】:Error EXC_BAD_ACCESS when pressing button 【发布时间】:2014-10-26 15:06:11 【问题描述】:当我按下按钮时,出现访问错误。 错误:线程 1:EXC_BAD_ACCESS(code=1,address=0xd0a937db)
TTest.h
@interface TTtest : NSObject
UIButton *monBouton ;
UIImage *skin;
- (void)initTest :(UIView*)vueDonne;
-(void)test:(id)sender;
TTest.m
- (void)initTest :(UIView*)vueDonne
skin = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"boutonG" ofType:@"png"]];
monBouton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
monBouton.frame = CGRectMake(50, 50, 45, 50);
[monBouton setImage:skin forState:UIControlStateNormal];
[monBouton addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchDown];
[vueDonne addSubview: monBouton];
-(void)test:(id)sender //didn't work because i have the probleme
NSLog(@"test clicked");
testViewController.m
- (void)viewDidLoad
[super viewDidLoad];
TTtest *test =[[TTtest alloc] init];
[test initTest:_testView]; //View of my application
编辑: 如果我添加 monBouton = [[UIButton alloc] init]; 我遇到了 SIGABRT 的问题
2014-10-26 16:47:22.827 testAsk[2134:a0b] -[CALayerArray test:]: unrecognized selector sent to instance 0xa141ea0
2014-10-26 16:47:22.831 testAsk[2134:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayerArray test:]: unrecognized selector sent to instance 0xa141ea0'
*** First throw call stack:
(
0 CoreFoundation 0x017395e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014bc8b6 objc_exception_throw + 44
2 CoreFoundation 0x017d6903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0172990b ___forwarding___ + 1019
4 CoreFoundation 0x017294ee _CF_forwarding_prep_0 + 14
5 libobjc.A.dylib 0x014ce874 -[NSObject performSelector:withObject:withObject:] + 77
6 UIKit 0x0022c0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
7 UIKit 0x0022c04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
8 UIKit 0x003240c1 -[UIControl sendAction:to:forEvent:] + 66
9 UIKit 0x00324484 -[UIControl _sendActionsForEvents:withEvent:] + 577
10 UIKit 0x003231fd -[UIControl touchesBegan:withEvent:] + 254
11 UIKit 0x0026934b -[UIWindow _sendTouchesForEvent:] + 386
12 UIKit 0x0026a184 -[UIWindow sendEvent:] + 1232
13 UIKit 0x0023de86 -[UIApplication sendEvent:] + 242
14 UIKit 0x0022818f _UIApplicationHandleEventQueue + 11421
15 CoreFoundation 0x016c283f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
16 CoreFoundation 0x016c21cb __CFRunLoopDoSources0 + 235
17 CoreFoundation 0x016df29e __CFRunLoopRun + 910
18 CoreFoundation 0x016deac3 CFRunLoopRunSpecific + 467
19 CoreFoundation 0x016de8db CFRunLoopRunInMode + 123
20 GraphicsServices 0x0368e9e2 GSEventRunModal + 192
21 GraphicsServices 0x0368e809 GSEventRun + 104
22 UIKit 0x0022ad3b UIApplicationMain + 1225
23 testAsk 0x000027bd main + 141
24 libdyld.dylib 0x01d75725 start + 0
25 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
【问题讨论】:
从外观上看,我认为您需要先实例化 UIButtonmonBouton = [[UIButton alloc] init]
。试一试。
如果我添加 monBouton =[[UIButton alloc] init] 我遇到了 SIGBART 的问题,我将文本放在我的帖子中
【参考方案1】:
崩溃的原因是测试对象在收到按钮动作时被释放。
TTtest *test =[[TTtest alloc] init]; //dealloced after viewDidLoad
所以尝试将测试设为属性并使用 self.test。
self.test =[[TTtest alloc] init];
【讨论】:
是的!如何 ?我不明白为什么在它不起作用之前。你能解释一下吗? 那么它现在工作了吗?因为原来的测试对象是一个本地对象,它只存在于viewDidLoad方法中,超出范围,它被释放以节省内存使用,你的目标动作-(void)test:(id)sender是在测试对象中定义的,当您单击按钮时不再存在。【参考方案2】:看起来你没有定义选择器“test”
-(void)test: (id) sender
NSLog(@"test clicked");
【讨论】:
是的,我忘记了,但是由于“访问不正确”,该方法不起作用。以上是关于按下按钮时出现错误 EXC_BAD_ACCESS的主要内容,如果未能解决你的问题,请参考以下文章