【问题标题】:dealloc after instantiateViewControllerWithIdentifier在 instantiateViewControllerWithIdentifier 之后解除分配
【发布时间】:2013-10-09 11:44:10
【问题描述】:

我有一个问题:

ExploreViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ProfileViewController"];
vc.id_from = post.user_id;
[self.navigationController pushViewController:vc animated:YES];

如您所见,我实例化了 viewController 并将其推送到 navigationController 中。 vc,应该是 autorelease ,但从不调用 dealloc 方法。

所以,如果我在推送后释放视图控制器:

ExploreViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ProfileViewController"];
vc.id_from = post.user_id;
[self.navigationController pushViewController:vc animated:YES];
[vc release];

当我弹出视图控制器时会调用dealloc方法,但是如果我再次执行上面的代码,会立即调用dealloc并且应用程序崩溃,因为其他对象没有找到vc。

所以,如果我不释放它,内存会越来越忙。

谢谢大家!

【问题讨论】:

    标签: ios objective-c storyboard release dealloc


    【解决方案1】:

    它没有发布,因为当你:

    [self.navigationController pushViewController:vc animated:YES];
    

    UINavigationController 引用了vc。所以基本上你有2个引用:

    self.navigationController + ExploreViewController *vc = 2
    

    在方法的最后你有一个:

    self.navigationController = 1
    

    一旦你从UINavigationController 弹出vcvc 应该被释放并且dealloc 方法被调用。另一件事,你不应该在你不拥有的对象上调用release。在这种情况下,instantiateViewControllerWithIdentifier 返回一个自动释放对象。

    【讨论】:

      【解决方案2】:

      您应该只在 alloc、new 或 copy 之后对对象调用 release。在这种情况下,你不应该调用[vc release].

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-12
        • 1970-01-01
        • 2020-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多