iOS6 viewDidUnload已过时

编程入门 行业动态 更新时间:2024-10-22 23:04:32
本文介绍了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已过时

本文发布于:2023-11-14 10:11:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586933.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:viewDidUnload

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!