【发布时间】:2012-03-12 12:26:23
【问题描述】:
我是 ARC 的新手,我玩它还不到一周。我想做的是非常基本的。我有一个显示按钮的视图控制器。当我单击按钮时,需要调用相应的选择器。但是,使用 ARC 时,应用程序因 EXC_BAD_ACCESS 消息而崩溃。下面是我的 MainViewController 中的代码
- (void)loadView
{
[super loadView];
UIButton *testButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[testButton setFrame:CGRectMake(80,50,160,50)];
[testButton setTitle:@"Click Me" forState:UIControlStateNormal];
[testButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:testButton];
}
-(void)buttonClicked:(id)sender
{
NSLog(@"Button Clicked");
}
我启用了 Zombie Objects 并且能够在调试日志中找到以下消息
2012-02-21 22:46:00.911 test[2601:f803] *** -[MainViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x6b4bba0
查看上面的消息,在我看来 ARC 过早地释放了我的 MainViewController。 我不确定我在这里做错了什么。如果我遗漏了什么,请告诉我。
提前致谢
【问题讨论】:
-
如果没有代码,VC 是如何初始化的,是无法判断的。我猜你只是没有强烈的参考。
标签: iphone ios memory-management automatic-ref-counting