应该将viewDidUnload中的哪些内容移动到didReceiveMemoryWarning? [英] What works in viewDidUnload should be moved to didReceiveMemoryWarning?

查看:131
本文介绍了应该将viewDidUnload中的哪些内容移动到didReceiveMemoryWarning?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在新版iOS 6中,不推荐使用 viewDidUnload ,我们已被指示使用 didReceiveMemoryWarning 代替管理对象在UIViewController实例和子类中。在 didReceiveMemoryWarning 中将nils分配给UIView类似于在 viewDidUnload 中完成它的方式同样有效吗?

In new iOS 6, viewDidUnload is deprecated and we have been instructed to use didReceiveMemoryWarning instead, to manage objects in UIViewController instances and subclasses. Is it equally effective to assign nils to UIView kinds inside didReceiveMemoryWarning like the way it has been done inside viewDidUnload?

我问这个是因为这两种方法似乎有所不同。似乎 didReceiveMemoryWarning 不保证再次调用 viewDidLoad 来重新实例化任何必要的UIView。

I am asking this because these two methods seems to be working differently. It seems like didReceiveMemoryWarning doesn't guarantee viewDidLoad to be called again to re-instantiate any necessary UIViews.

我怀疑在iOS 6中,内存管理完成后无需手动解除分配UIView。请帮助我了解在理解UIViewController的生命周期时我错过了什么。

I suspect with iOS 6, memory management is done without requiring to manually deallocate UIView. Please help me to know what I have missed in understanding the lifecycle of UIViewController.

推荐答案

我首选的方法如下:

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if (self.isViewLoaded && !self.view.window) {
        self.view = nil;
    }
    // Do additional cleanup if necessary
}

请注意,测试 self.isViewLoaded 是必不可少的,否则访问视图会导致它加载 - 即使WWDC视频也会错过。

Note that the test self.isViewLoaded is essential, as otherwise accessing the view causes it to load - even the WWDC videos tend to miss that.

如果你对子视图的其他引用是弱引用,你不必在这里取消它们,否则你也想把它们设置为nil。

If your other references to subviews are weak references, you don't have to nil them out here, otherwise you want to set them to nil, too.

你应该完全摆脱 viewDidUnload ,那里的每个代码都应该移动到适当的位置。无论如何,无法保证在iOS 6之前调用它。

You should get rid of viewDidUnload completely, and every code there should move to appropriate places. It wasn't guaranteed to be called prior to iOS 6 before anyway.

这篇关于应该将viewDidUnload中的哪些内容移动到didReceiveMemoryWarning?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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