2014-10-26 63 views
1

当我按下我的按钮时,出现访问错误。 错误:线程1:EXC_BAD_ACCESS(码= 1,地址= 0xd0a937db)按下按钮时出错EXC_BAD_ACCESS

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) 
+0

从它的外观来看,我认为你需要首先实例化UIButton'monBouton = [[UIButton alloc] init]'。试一试。 – carlodurso 2014-10-26 15:42:43

+0

如果我添加了monBouton = [[UIButton alloc] init] 我遇到了一个SIGBART的问题,我把文本放在我的文章中 – Supernirito 2014-10-26 15:48:12

回答

0

的原因崩溃的是,测试对象在接收到按钮动作dealloced 。

TTtest *test =[[TTtest alloc] init]; //dealloced after viewDidLoad 

所以尽量使测试属性和使用self.test。

self.test =[[TTtest alloc] init]; 
+0

是的!怎么样 ?我不明白为什么在它不起作用之前。你能解释一下吗? – Supernirito 2014-10-26 16:01:30

+0

现在它在工作吗?因为原来的测试对象是一个本地对象,它只存在于viewDidLoad方法中,超出范围,它被释放以节省内存使用,你的目标动作 - (void)test:(id)sender是在测试对象中定义的,当你点击按钮时不再存在。 – gabbler 2014-10-26 16:04:51

0

的问题并不像你所定义的选择“测试”

-(void)test: (id) sender 
    NSLog(@"test clicked");  
} 
+0

是的,我忘记了,但方法不起作用的原因是“访问不良”。 – Supernirito 2014-10-26 15:28:22