UIViewController -dealloc方法未调用 [英] UIViewController -dealloc method not called

查看:166
本文介绍了UIViewController -dealloc方法未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自动参考计数。
我有一个自定义 UIViewController 子类,每当我调用 -presentViewController:animated:completion:或删除它从superview查看我想 NSLog 类似我被释放的东西,所以我知道视图控制器已成功删除。我在视图控制器中实现了 -dealloc 方法。但是我开始了一个测试项目,我只有两个 UIViewController 实例(没有保留周期),并且没有调用 -dealloc 当我按模式推送第二个 UIViewController 或者当我删除超级视图或从父视图控制器中删除它时。我错过了什么吗?在我原来的项目中(不是测试用例),仪器向我展示了那些控制器留下的内存空间是我无法摆脱的。

I am working with Automatic Reference Counting. I have a custom UIViewController subclass and whenever I call -presentViewController: animated:completion: or remove its view from the superview I would like to NSLog something like "I am dealloced" so I know that the view controller has successfully been removed. I have implemented the -dealloc method in my view controller. However I started a test project where I just had two UIViewController instances (no retain cycles) and -dealloc is not called either when I push the second UIViewController modally or when I remove the superview or when I remove it from the parent view controller. Am I missing something ? In my original project (not the test case) Instruments shows me that those controllers leave a memory footprint that I can't get rid off.

推荐答案

如果要切换视图控制器,并将要切换的视图控制器取消分配,则只需切换窗口的根视图控制器。所以,如果你在VC1并希望转到VC2,那么在VC1中执行此操作:

If you want to switch view controllers, and have the one you're switching away from be deallocated, then just switch the root view controller of the window. So, if you're in VC1 and want to go to VC2, then do this in VC1:

VC2 *vc2 = [[VC2 alloc] init]; // or however else is appropriate to get an instance of this class
self.view.window.rootViewController = vc2;

如果你没有创建任何属性指向vc1,那么在完成此操作后它将被取消分配开关。

If you haven't created any property to point to vc1, then it will be deallocated after making this switch.

如果你想使用模态演示或模态segue(在切换控制器时获取动画),你仍然可以通过切换来取消分配初始控制器从vc2的viewDidAppear方法演示后的根视图控制器:

If you want to use a modal presentation or a modal segue (to get the animation when you switch controllers), you can still get the initial controller to be deallocated by switching the root view controller after the presentation from the viewDidAppear method of vc2:

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.view.window.rootViewController = self;
}

这篇关于UIViewController -dealloc方法未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆