我在ARC下编写代码,使用这种方法我总是得到EXC_BAD_ACCESS

时间:2012-09-20 00:17:44

标签: objective-c automatic-ref-counting

我创建了NavigationItem,而BarButtonItem使用items.leftBarButtonItem来添加,而baritem的动作我使用@selector来处理,它可以编译得很好但是当我触摸buttonitem时,它会崩溃..可能是选择器导致它。 当我在配置文件上关闭ARC时,它向右转......为什么? 我对ARC感到困惑,有谁能告诉我如何处理它?

@implementation IGMainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 340, 44)];

    UINavigationItem  *items = [[UINavigationItem alloc] init];
    UIBarButtonItem  *leftBtn = [[UIBarButtonItem alloc] initWithTitle:@"aa" style:UIBarButtonSystemItemAdd target:self action:@selector(doItAgain:)];
    items.leftBarButtonItem = leftBtn;
    [navBar pushNavigationItem:items animated:NO];
    [self.view addSubview:navBar];
}
-(void)doItAgain:(id)sender
{
    NSLog(@"sss");
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

//固定版本:

    //old:
    IGMainViewController *mview = [[IGMainViewController alloc] init];
    [self.view addSubview:mview.view];
    //fixed:
    @property(strong,nonatomic) IGMainViewController *mview;

1 个答案:

答案 0 :(得分:2)

@selector(doItAgain)表示doItAgain方法不带参数,而您的方法需要1.要解决此问题,请在选择器名称中添加冒号:@selector(doItAgain:)