ARC 过早发布 ViewController
我是 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 对象,并能够在调试日志中找到以下消息。
2012-02-21 22:46:00.911 test[2601:f803] *** -[MainViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x6b4bba0
查看上面的消息,在我看来,ARC 过早地释放了我的 MainViewController。 我不确定我在这里做错了什么。如果我遗漏了什么,请告诉我。
提前致谢
I am new to ARC and I have been playing with it for less than a week. What I am trying to do is very basic. I have a view controller that displays a button. When I click the button, the corresponding selector needs to be called. However, with ARC, the application crashed with an EXC_BAD_ACCESS message. Below is the code from my 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");
}
I enabled Zombie Objects and was able to find the following message in the debug logs
2012-02-21 22:46:00.911 test[2601:f803] *** -[MainViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x6b4bba0
Looking at the above message, it seems to me that ARC is prematurely releasing my MainViewController.
I am not sure what I am doing wrong here. Please let me know if I am missing something.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请使用strong键
Please use the strong key
如果其他人有与此类似的症状,但在我的实例中使用故事板和 Segues - 这个答案对我有帮助:
IOS 5 消息发送到 alloc 命令上的已释放实例
修复是将我的 MKMapView 的委托设置为 nil,期间视图将消失。花了很长时间才找到解决方案!
In case anyone else has a similar symptoms to this, but is using Storyboards and Segues as in my instance - this answer helped me:
IOS 5 message sent to deallocated instance on alloc command
The fix was to set the delegate of my MKMapView to nil, during viewWillDisappear. Took ages to find that solution!