iOS6 viewDidUnload已过时 [英] iOS6 viewDidUnload Deprecated

查看:121
本文介绍了iOS6 viewDidUnload已过时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是一个不好的做法,但是根据我阅读的文档,我得到了在viewDidLoad方法中的某些情况下初始化对象的建议,并且在viewDidUnload中为nil它。

Maybe this is a bad practice, but from the documentations that I read I got the advice to initialize objects in some cases inside the viewDidLoad method and nil it in viewDidUnload.

例如,如果您有类似添加观察者的内容

For example if you have something like adding an Observer

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(filterready:)
                                                 name:@"filterReady"
                                               object:nil];

现在我没有删除Observer的方法,但每次调用viewDidLoad时都会调用它显示的视图导致多个观察者在一段时间后运行,然后多次调用选择器。

Now I don't have a method to remove the Observer, however the viewDidLoad becomes called every time the view is shown which results in having multiple observers running after a while and the selector is then called multiple times.

我可以通过将一些清洁器移动到viewDidDisappear方法来解决这个问题,但是现在我有一些疑问,如果我做的是正确的。

I can fix this by moving some cleaners into the viewDidDisappear method, but now I have some doubts if I'm doing the right thing.

在我的示例中,我有多个控制其子导航的导航控制器,但dealloc从不被调用对于他们,即使他们没有被引用

In my sample I have multiple Navigation Controllers that are controlling their subnavigations, but the dealloc is never called for them, even though they are not referenced

推荐答案

你应该使用
- ( void)didReceiveMemoryWarning

- (void)dealloc methods。

You should use the - (void)didReceiveMemoryWarning and - (void)dealloc methods.


在iOS 6中,现在不推荐使用UIViewController的viewWillUnload和viewDidUnload方法。如果您使用这些方法来释放数据,请改用didReceiveMemoryWarning方法。如果未使用此方法,也可以使用此方法释放对视图控制器视图的引用。在执行此操作之前,您需要测试视图是否在窗口中。

In iOS 6, the viewWillUnload and viewDidUnload methods of UIViewController are now deprecated. If you were using these methods to release data, use the didReceiveMemoryWarning method instead. You can also use this method to release references to the view controller’s view if it is not being used. You would need to test that the view is not in a window before doing this.

所以你应该检查你的视图是否在首先是窗口,然后在 didReceiveMemoryWarning

So you should check if your view is in the window first, then remove your observer in the didReceiveMemoryWarning

这篇关于iOS6 viewDidUnload已过时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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