按下按钮时出现EXC_BAD_ACCESS错误

时间:2014-10-26 15:06:12

标签: ios objective-c uibutton

当我按下我的按钮时,我收到了访问错误。 错误:线程1:EXC_BAD_ACCESS(代码= 1,地址= 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)

2 个答案:

答案 0 :(得分:0)

看起来你没有定义选择器" test"

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

答案 1 :(得分:0)

崩溃的原因是测试对象在收到按钮操作时被释放。

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

所以尝试将测试作为属性并使用self.test。

self.test =[[TTtest alloc] init];
相关问题