本文介绍了拆除 UIViewController 时是否总是调用 viewDidUnload 和 dealloc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 viewDidUnloaddealloc 是否在 UIViewController 拆卸过程中总是被连续调用.是否有可能在我的视图控制器上调用 dealloc 而不先调用 viewDidUnload?

I'd like to know whether or not both viewDidUnload and dealloc are always called in succession in the UIViewController tear-down process. Is it possible that dealloc could be called on my view controller without viewDidUnload having been called first?

在任何一种情况下,如果我在两种方法中安全地释放属性和保留引用,那么调用这两种方法都不会成为问题——但我想知道是否有人确切知道或可以阐明拆解过程.

In either case, if I am safely releasing the properties and retained references in both methods it wouldn't be a problem if both methods were called -- but I was wondering if anyone knew for sure or could shed some light on the tear-down process.

2012 更新: 需要注意的是,好像 iOS 6 viewDidUnload 已被弃用,应该替换为手动视图拆卸如果需要didReceiveMemoryWarning 中.

2012 Update: It's handy to note that as if iOS 6 viewDidUnload has been deprecated and should be replaced with manual view teardown if required in didReceiveMemoryWarning.

一篇关于新 UIView/UIViewContoller 和新行为的好文章,以及它对 乔康威博客

A good article on the new UIView/UIViewContoller and the new behaviour and it's effects on the joe conway blog

推荐答案

viewDidUnload 不会dealloc方法那样每次都被调用.viewDidUnload 仅在您的应用收到内存不足警告时调用!

viewDidUnload will not be called every time like dealloc method. viewDidUnload is called only when your app receives low memory warning!

试想一下,如果你同时在 viewDidUnloaddealloc 方法中释放你的对象.如果每次都调用两者,那么您正在释放已经释放的对象,这将导致应用程序崩溃,不是吗?.viewDidUnload 是 Apple 提供的用于在收到内存不足警告时清理东西的地方,因为您知道在 iPhone 中我们有内存限制.

Just think, if you are releasing your object both in viewDidUnload and dealloc methods. If both gets called every time, then you are releasing already released object, which will lead to application crash, isn't it?. viewDidUnload is a place provided by the Apple for cleaning up the things when receiving the low memory warning because you know in iPhone we have memory restriction.

这篇关于拆除 UIViewController 时是否总是调用 viewDidUnload 和 dealloc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 23:15