iphone – 我必须在viewDidUnload中做什么?

我倾向于在-dealloc中发布我的东西,现在iPhone OS 3.0引入了这个有趣的-viewDidUnload方法,他们说:

// Release any retained subviews of
the main view. // e.g. self.myOutlet
= nil;

所以,当视图控制器的视图从内存中被删除时,似乎被调用了。如果我将视图附加到视图控制器的主视图,我必须在这里发布这些东西,但不能在-dealloc中发布?

这很混乱另外,如果-dealloc会导致视图被卸载(释放)呢?那么再次会调用-viewDidUnload?

我确实意识到差异,-viewDidUnload只是为了视图本身被杀死的情况,而视图控制器留在内存中。而–dealloc是整个事情都是垃圾的情况。

也许有人可以清除混乱。

这里的意图是“平衡”您的子视图管理。您在viewDidLoad中创建的任何内容都应在viewDidUnload中释放。这使得更容易跟踪应该在哪里发布。在大多数情况下,您的dealloc方法是您的init方法的镜像,您的viewDidUnload将是您的viewDidLoad方法的镜像。

正如您指出的那样,viewDid …方法将在视图本身加载和卸载时使用。这允许视图控制器保持加载到存储器中的使用模式,但视图本身可以根据需要加载和卸载:

init
viewDidLoad
viewDidUnload
viewDidLoad
viewDidUnload
...
dealloc

当然,只要在viewDidUnload中释放它们时,只要将其设置为nil,就不会在您的dealloc方法中释放任何东西。

苹果UIViewController documentation的内存管理部分的以下引用更详细地描述:

…in iPhone OS 3.0 and later, the viewDidUnload method may be a more appropriate place for most needs.

When a low-memory warning occurs, the UIViewController class purges its views if it knows it can reload or recreate them again later. If this happens, it also calls the viewDidUnload method to give your code a chance to relinquish ownership of any objects that are associated with your view hierarchy, including objects loaded with the nib file, objects created in your viewDidLoad method, and objects created lazily at runtime and added to the view hierarchy. Typically, if your view controller contains outlets (properties or raw variables that contain the IBOutlet keyword), you should use the viewDidUnload method to relinquish ownership of those outlets or any other view-related data that you no longer need.

http://stackoverflow.com/questions/2261972/what-exactly-must-i-do-in-viewdidunload

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:iphone – 我必须在viewDidUnload中做什么?