我什么时候应该在 -(void)viewDidUnload 而不是在 -dealloc 中释放对象? [英] When should I release objects in -(void)viewDidUnload rather than in -dealloc?

查看:26
本文介绍了我什么时候应该在 -(void)viewDidUnload 而不是在 -dealloc 中释放对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-(void)viewDidUnload 有什么用?

我可以不只是在 -dealloc 中释放所有内容吗?如果视图确实卸载了,-dealloc 不会被调用吗?

Could I not just relase everything in -dealloc? If the view did unload, wouldn't -dealloc be called anyway?

推荐答案

除了已经指出的内容之外,我还想详细说明 -viewDidUnload 背后的逻辑.

In addition to what has already been indicated, I wanted to elaborate more about logic behind -viewDidUnload.

实现它的最重要原因之一是 UIViewController 子类通常还包含对视图层次结构中各种子视图的拥有引用.例如,这些属性可以在从笔尖加载时通过 IBOutlets 设置,或者在 -loadView 中以编程方式设置.

One of the most important reasons for implementing it is that UIViewController subclasses commonly also contain owning references to various subviews in the view hierarchy. These properties could have been set through IBOutlets when loading from a nib, or programmatically inside -loadView, for instance.

UIViewController 对子视图的额外所有权意味着即使它的视图从视图层次结构中移除并释放以节省内存,通过这种方式子视图也被视图释放,它们不会实际上被释放是因为 UIViewController 本身仍然包含它自己对这些对象的未完成的保留引用.释放这些对象的 UIViewController 额外所有权可确保它们也将被释放以释放内存.

The additional ownership of subviews by the UIViewController means that even when its view is removed from the view hierarchy and released to save memory, through which the subviews are also released by the view, they will not actually be deallocated because the UIViewController itself still contains its own outstanding retaining references to those objects as well. Releasing the UIViewController additional ownership of these objects ensures they will be deallocated as well to free memory.

UIViewController 视图被重新加载 时,您在此处释放的对象通常会重新创建和设置,无论是从 Nib 还是通过 的实现-loadView.

The objects that you release here are usually recreated and set again when the UIViewController view is re-loaded, either from a Nib or through an implementation of -loadView.

另请注意,在调用此方法时,UIViewController view 属性为 nil.

Also note that the UIViewController view property is nil by the time this method is called.

这篇关于我什么时候应该在 -(void)viewDidUnload 而不是在 -dealloc 中释放对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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