didReceiveMemoryWarning(在 iOS 3.0 中)
查看 didReceiveMemoryWarning 的文档:
注意它是怎么说的,我 引用:
在 iOS 3.0 及更高版本中,如果您认为 控制器保存对象的引用 在视图层次结构中,您应该 发布这些参考文献 改为 viewDidUnload 方法。在 早期版本的 iOS,您应该 继续将他们从这件事中释放出来 方法。
为什么会这样呢? iOS 3.0 中发生了什么变化,使得视图层次结构视图不能直接在 didReceiveMemoryWarning 中清理?我无法想象什么会导致这种情况变得危险或糟糕。
大家有什么想法吗?
Have a peek at the documentation for didReceiveMemoryWarning:
Note how it says, and I quote:
In iOS 3.0 and later, if your view
controller holds references to objects
in the view hierarchy, you should
release those references in the
viewDidUnload method instead. In
earlier versions of iOS, you should
continue to release them from this
method.
Why is this the case? What changed in iOS 3.0 that made it so that view-hierarchy views must not be cleaned up directly in didReceiveMemoryWarning? I can't imagine what could possibly make that dangerous or bad.
Any ideas guys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 iOS 3.0 中引入了
viewDidUnload
和viewDidLoad
。如果你看一下他们的描述,你会发现:
这意味着,当您的视图从 Nib 加载时,或者当您以编程方式创建视图时(并且框架在正确的时刻
loadView
调用您),您都可以通过一个点访问您的新视图并完成其初始化,例如添加子视图或您需要的任何内容。viewDidLoad
的对应部分是viewDidUnload
,您可以像这样覆盖它:因此,您有一个单点进行清理,并且不需要执行任何特定的清理-up 在
didReceiveMemoryWarning
中,因为每当视图被释放时都会调用viewDidUnload
,即,当由于以下原因释放视图时也会调用didReceiveMemoryWarning
。这与 iOS 3.0 之前发生的情况不同,在 iOS 3.0 中,您必须在没有框架支持的情况下制定自己的方案来完成初始化和清理,即当
didReceiveMemoryWarning
导致视图被释放后,您的清理方法不会自动调用,您必须复制清理代码(并在didReceiveMemoryWarning
中显式执行清理)。In iOS 3.0
viewDidUnload
andviewDidLoad
were introduced.If you look at their description, you'll see that:
This means that, both when your view is loaded from a Nib, or when you create it programmatically (and the framework calls for you at the right moment
loadView
), you have a single point where you can access your new view and complete its initialization, like adding subviews, or whatever you need.The counterpart to
viewDidLoad
isviewDidUnload
that you can override like this:so, you have one single point for clean-up, and you don't need to do any specific clean-up in
didReceiveMemoryWarning
, becauseviewDidUnload
is called whenever a view is deallocated, i.e., also when it is deallocated due todidReceiveMemoryWarning
.This is different to what happened previously to iOS 3.0, where you had to come out with your own scheme for completing initialization and clean-up with no support from the framework, i.e. when
didReceiveMemoryWarning
caused a view to be deallocated, your clean-up method was not automatically called and you had to duplicate your clean-up code (and explicitly do the clean-up indidReceiveMemoryWarning
).