在 viewDidUnload 中我到底必须做什么? [英] What exactly must I do in viewDidUnload?

查看:12
本文介绍了在 viewDidUnload 中我到底必须做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tend to release my stuff in -dealloc, and now iPhone OS 3.0 introduced this funny -viewDidUnload method, where they say:

//释放所有保留的子视图主要观点.//例如self.myOutlet= 零;

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

所以-viewDidUnload 似乎在视图控制器的视图已从内存中启动时被调用.如果我将子视图附加到视图控制器的主视图,我必须只在此处发布这些内容,而不能在 -dealloc 中发布?

So -viewDidUnload seems to get called when the view of the view controller has been kicked off from memory. And if I have subviews attached to the main view of the view controller, I have to release that stuff only HERE, but not in -dealloc as well?

这令人困惑.另外,如果 -dealloc 导致视图被卸载(释放)怎么办?再说一次,它会调用-viewDidUnload?

That's confusing. Also, what if -dealloc causes the view to be unloaded (released)? Then again, it will call -viewDidUnload?

我确实意识到了不同之处,-viewDidUnload 仅适用于视图本身被杀死但视图控制器保留在内存中的情况.而 -dealloc 是针对整个事情都被扔进垃圾桶的情况.

I do realize the difference, that -viewDidUnload is just for the case where the view itself gets killed, but the view controller stays in memory. And -dealloc is for the case where the whole thing goes to trash.

也许有人可以解决这个困惑.

Maybe someone can clear up the confusion.

推荐答案

这里的目的是平衡"您的子视图管理.您在 viewDidLoad 中创建的任何内容都应该在 viewDidUnload 中发布.这样可以更轻松地跟踪应该在何处发布的内容.在大多数情况下,您的 dealloc 方法是您的 init 方法的镜像,而您的 viewDidUnload 将是您的 的镜像>viewDidLoad 方法.

The intent here is to "balance out" your subview management. Anything that you create in viewDidLoad should be released in viewDidUnload. This makes it easier to keep track of what should be released where. In most cases, your dealloc method is a mirror-image of your init method, and your viewDidUnload will be a mirror image of your viewDidLoad method.

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

As you pointed out, the viewDid... methods are to be used when the view itself is loaded and unloaded. This permits a usage pattern in which the view controller remains loaded in memory, but the view itself can be loaded and unloaded as required:

init
viewDidLoad
viewDidUnload
viewDidLoad
viewDidUnload
...
dealloc

当然,在你的 dealloc 方法中释放东西也没有什么坏处,只要你在 释放它们时将它们设置为 nil>viewDidUnload.

Of course, it doesn't hurt to release things in your dealloc method as well, as long as you set them to nil when you release them in viewDidUnload.

Apple 的内存管理部分的以下引用 UIViewController 文档,对其进行了更详细的描述:

The following quote from the Memory Management section of Apple's UIViewController documentation, describes it in more detail:

...在 iPhone OS 3.0 及更高版本中,viewDidUnload 方法可能更适合大多数需求.

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

当发生内存不足警告时,UIViewController 类会清除其视图,如果它知道它可以稍后重新加载或重新创建它们.如果发生这种情况,它还会调用 viewDidUnload 方法,让您的代码有机会放弃与您的视图层次结构相关联的任何对象的所有权,包括使用 nib 文件加载的对象、在您的 viewDidLoad 方法中创建的对象以及延迟创建的对象运行时并添加到视图层次结构中.通常,如果您的视图控制器包含出口(包含 IBOutlet 关键字的属性或原始变量),您应该使用 viewDidUnload 方法放弃这些出口或您不再需要的任何其他与视图相关的数据的所有权.

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.

这篇关于在 viewDidUnload 中我到底必须做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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