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

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

问题描述

- (void)viewDidUnload 是什么?



-dealloc

解决方案

如果视图已卸载,则不会调用 -dealloc

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



实现它的最重要的原因之一是 UIViewController 子类通常也包含拥有对各种子视图的引用视图层次结构。这些属性可以通过 IBOutlets 在从nib加载时设置,或以编程方式在 -loadView 内设置。 / p>

UIViewController 对子视图的额外所有权意味着即使其视图从视图层次结构中删除,由于 UIViewController 本身仍然包含对这些对象的自己的突出的保留引用,因此通过视图释放子视图的保存内存实际上不会被释放。释放这些对象的 UIViewController 附加所有权确保它们被释放以释放内存。



你在这里的发布通常是重新创建和设置,当 UIViewController 视图重新加载,从一个Nib或通过 -loadView



另请注意, UIViewController 查看属性为 nil


What is the -(void)viewDidUnload is good for?

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

解决方案

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

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.

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.

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.

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

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

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