Bad access exception using xcode 4.2

2019-08-11 08:26发布

I am getting "EXC_BAD_ACCESS" when touch the button to call the button action.

#import "TestViewController.h"

@implementation TestViewController

- (id)init
{
    self = [super init];
    if (self) 
    {
       self.title=@"IOS5 and Xoced 4.2";

        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(9,6,100,100)];
        button.backgroundColor=[UIColor greenColor];
        [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview: button];


    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithTitle:@"Add" 
                                                                 style:UIBarButtonItemStyleBordered 
                                                                target:self 
                                                                action:@selector(addAction)];
    self.navigationItem.rightBarButtonItem = addButton;

}
-(void)buttonAction
{
    printf("Hi i am in buttonAction method");

}

-(void)addAction
{
    printf("Hi i am in addAction method");

}

I used ARC enbled in this project. When I touch the UIBarButtonItem or UIButton I am getting below error.

TestXcode4[2470:207] -[__NSCFString addAction]: unrecognized selector sent to instance 0x6827b00

Please help me out of this.

2条回答
萌系小妹纸
2楼-- · 2019-08-11 08:30

If you add this view controller to the window then you don't release that controller after addsubview call. Because window will not retain that view controller.

查看更多
成全新的幸福
3楼-- · 2019-08-11 08:35

I just disable the ARC in project setting. then it is working fine....

查看更多
登录 后发表回答