didReceiveMemoryWarning(在 iOS 3.0 中)

发布于 2024-11-08 19:44:54 字数 617 浏览 0 评论 0原文

查看 didReceiveMemoryWarning 的文档:

http ://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

注意它是怎么说的,我 引用:

在 iOS 3.0 及更高版本中,如果您认为 控制器保存对象的引用 在视图层次结构中,您应该 发布这些参考文献 改为 viewDidUnload 方法。在 早期版本的 iOS,您应该 继续将他们从这件事中释放出来 方法。

为什么会这样呢? iOS 3.0 中发生了什么变化,使得视图层次结构视图不能直接在 didReceiveMemoryWarning 中清理?我无法想象什么会导致这种情况变得危险或糟糕。

大家有什么想法吗?

Have a peek at the documentation for didReceiveMemoryWarning:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

Note how it says, and I quote:

In iOS 3.0 and later, if your view
controller holds references to objects
in the view hierarchy, you should
release those references in the
viewDidUnload method instead. In
earlier versions of iOS, you should
continue to release them from this
method.

Why is this the case? What changed in iOS 3.0 that made it so that view-hierarchy views must not be cleaned up directly in didReceiveMemoryWarning? I can't imagine what could possibly make that dangerous or bad.

Any ideas guys?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹沉浮 2024-11-15 19:44:54

在 iOS 3.0 中引入了 viewDidUnloadviewDidLoad

如果你看一下他们的描述,你会发现:

viewDidLoad 在视图控制器将其关联的视图加载到内存后调用。无论视图是存储在 nib 文件中还是在 loadView 方法中以编程方式创建,都会调用此方法。

这意味着,当您的视图从 Nib 加载时,或者当您以编程方式创建视图时(并且框架在正确的时刻 loadView 调用您),您都可以通过一个点访问您的新视图并完成其初始化,例如添加子视图或您需要的任何内容。

viewDidLoad 的对应部分是 viewDidUnload,您可以像这样覆盖它:

-(void)viewDidUnload {
     <do all the necessary clean up>
    [super viewDidUnload];
}

因此,您有一个单点进行清理,并且不需要执行任何特定的清理-up 在 didReceiveMemoryWarning 中,因为每当视图被释放时都会调用 viewDidUnload ,即,当由于以下原因释放视图时也会调用didReceiveMemoryWarning

这与 iOS 3.0 之前发生的情况不同,在 iOS 3.0 中,您必须在没有框架支持的情况下制定自己的方案来完成初始化和清理,即当 didReceiveMemoryWarning 导致视图被释放后,您的清理方法不会自动调用,您必须复制清理代码(并在 didReceiveMemoryWarning 中显式执行清理)。

In iOS 3.0 viewDidUnload and viewDidLoad were introduced.

If you look at their description, you'll see that:

viewDidLoad called after the view controller has loaded its associated views into memory. This method is called regardless of whether the views were stored in a nib file or created programmatically in the loadView method.

This means that, both when your view is loaded from a Nib, or when you create it programmatically (and the framework calls for you at the right moment loadView), you have a single point where you can access your new view and complete its initialization, like adding subviews, or whatever you need.

The counterpart to viewDidLoad is viewDidUnload that you can override like this:

-(void)viewDidUnload {
     <do all the necessary clean up>
    [super viewDidUnload];
}

so, you have one single point for clean-up, and you don't need to do any specific clean-up in didReceiveMemoryWarning, because viewDidUnload is called whenever a view is deallocated, i.e., also when it is deallocated due to didReceiveMemoryWarning.

This is different to what happened previously to iOS 3.0, where you had to come out with your own scheme for completing initialization and clean-up with no support from the framework, i.e. when didReceiveMemoryWarning caused a view to be deallocated, your clean-up method was not automatically called and you had to duplicate your clean-up code (and explicitly do the clean-up in didReceiveMemoryWarning).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文