我需要调用[super viewDidUnload]吗?
我见过一些 Apple 示例确实调用了 [super viewDidUnload];
,而另一些则没有。我读过一篇文章(几个月前,所以我不记得网址了),说调用 [super viewDidUnload];
是不必要的,但除此之外没有解释。
是否有明确的理由或为何不告诉 super viewDidUnload
?
并且,(如果应该完成)我是否在将所有属性设置为nil
之前、之后调用super,还是重要?
- (void)viewDidUnload {
// Is this necessary?
// [super viewDidUnload];
self.tableDataSource = nil;
self.titleLabel = nil;
// Is it better to call super before or after nil'ing properties?
// [super viewDidUnload];
}
谢谢!
I have seen some Apple examples that do call [super viewDidUnload];
and some that don't. I read an article (a few months ago so I dont recall the url) that said calling [super viewDidUnload];
was unnecessary but it didn't explain beyond that.
Is there a definitive reason why or why not to tell super that the viewDidUnload
?
And, (if it should be done) do I call super before setting all my properties to nil
, after, or does it matter?
- (void)viewDidUnload {
// Is this necessary?
// [super viewDidUnload];
self.tableDataSource = nil;
self.titleLabel = nil;
// Is it better to call super before or after nil'ing properties?
// [super viewDidUnload];
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1-是否有明确的理由或为什么不告诉 super viewDidUnload?
老实说,我不知道不调用它的后果。你可以尝试不调用它,一切都会顺利进行,但想象一下苹果添加了一些重要的代码,这些代码将在调用
[super viewDidUnload]
时运行,现在会发生什么?可能会发生不好的事情,你会花费宝贵的时间来解决你的问题。我的规则是:当覆盖时,调用 super。2 - 我是否在将所有属性设置为 nil 之前、之后调用 super,还是有关系吗?
这确实很重要,当我调用 [
super dealloc] 在释放我的对象之前。同样,我看到 UI 速度很慢,因为我在
[super viewDidLoad]
之前进行了计算。这始终取决于您想要实现的目标。最重要的是,我在我的项目中为
viewDidUnload
所做的是:对于iOS6:
该方法已被弃用。
1- Is there a definitive reason why or why not to tell super that the viewDidUnload?
Honestly I don't know the consequences of not calling it. You can try to not call it and everything works smooth, but imagine that Apple adds some importante piece of code that will run when
[super viewDidUnload]
is called, what will happen now? Bad things will happen probably and you will spend precious time trying to solve your problem. My rule is: when overriding, call the super.2 - Do I call super before setting all my properties to nil, after, or does it matter?
It does matter, I have watched bad things happen when I called [
super dealloc]
before releasing my objects. The same way I saw my UI being slow because I did my calculations before[super viewDidLoad]
. It always depend of what you want to achieve.Bottom line, what I do in my projects for
viewDidUnload
is:As for iOS6:
The method has been deprecated.
viewDidUnload
与dealloc
类似,因为您正在“关闭”对象 - 您正在释放内存并将其置于(半)非活动状态。Cocoa 中推荐的模式是在子类的
dealloc
末尾执行[super dealloc]
因为您需要确保添加到类中的所有内容可以在您的实例因自身释放而失效之前被释放。同样的想法,尽管可能不是什么问题,也适用于 viewDidUnload。一般来说,创建的时候,让超类先工作。破坏时,让它最后发挥作用。
您不需要发送
[super deconstructionMethod]
当且仅当超类的实现不执行任何操作。我认为 viewDidUnload 就是这种情况,但我不确定,这表明了正确的方向:超类对你来说是不透明的,所以除非它有记录 > 它的实现什么也不做,你应该总是调用。viewDidUnload
is likedealloc
in that you are "shutting down" your object -- you're freeing up memory and putting it into a (semi-)inactive state.The recommended pattern in Cocoa is to do
[super dealloc]
at the end of your subclass'sdealloc
because you need to make sure that all the stuff you've added to the class can get released before your instance is invalidated by being freed itself. The same idea, although it is probably less of an issue, holds forviewDidUnload
.In general, when creating, let the superclass work first. When destroying, let it work last.
You don't need to send
[super deconstructionMethod]
iff the superclass's implementation does nothing. I think that is the case forviewDidUnload
, but I'm not sure, and that's kind of indicative of the proper direction: the superclass is opaque to you, so unless it's documented that its implementation does nothing, you should always call up.