使用xcode 4.2的访问异常

时间:2011-11-22 12:11:47

标签: iphone objective-c ios5 xcode4.2 automatic-ref-counting

触摸按钮调用按钮操作时,我收到“EXC_BAD_ACCESS”。

#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");

}

我在这个项目中使用了 ARC enbled 。 当我触摸UIBarButtonItem或UIButton时,我遇到了错误。

  

TestXcode4 [2470:207] - [__ NSCFString addAction]:无法识别的选择器   发送到实例0x6827b00

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

如果您将此视图控制器添加到窗口,则在添加子视图调用后您不会释放该控制器。因为窗口不会保留该视图控制器。

答案 1 :(得分:0)

我只是在项目设置中禁用ARC。那它工作正常....