ARC 过早发布 ViewController

发布于 2025-01-08 16:58:36 字数 941 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜唯美灬不弃 2025-01-15 16:58:36

请使用strong

@property (strong, nonatomic) MainViewController *mvc;

Please use the strong key

@property (strong, nonatomic) MainViewController *mvc;
云雾 2025-01-15 16:58:36

如果其他人有与此类似的症状,但在我的实例中使用故事板和 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文