我究竟必须在viewDidUnload中做些什么? [英] What exactly must I do in viewDidUnload?

查看:98
本文介绍了我究竟必须在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
= nil;

// 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.

正如你指出的那样out,在加载和卸载视图本身时使用 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 方法中释放内容也没有什么坏处当您在 viewDidUnload 中释放它们时, nil

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天全站免登陆