`[super viewDidLoad]`约定 [英] `[super viewDidLoad]` convention

查看:183
本文介绍了`[super viewDidLoad]`约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我知道你并不总是需要调用super(as)。见许多其他讨论)。
当你调用它时,它是预期之前或之后你的代码?



这可能会有影响,这取决于super的实现。



这当然适用于所有的UIViewControllers委托方法(willAppear,didAppear等etc ...) / p>

任何想法?

解决方案

我的经验法则是: '做一些与初始化相关的事情,总是先调用超类的方法(如果你打算调用它)。这让超级类有机会做任何设置,你可能会依赖于以后在你的方法。如果你正在做一些与销毁相关的事情,最后调用超类的方法。这确保您可以在整个方法的执行过程中依赖对象的状态。最后,根据具体情况采取任何其他情况。例如,如果你正在处理一个事件,你可能想先处理事件,并且只调用超类的方法,如果你选择不处理事件,或者如果你改变了它,并想沿着事件传递它链。


I see some example code with [super viewDidLoad] called before your implementation and after your implementation.

I know you don't always have to call super (as seen in many other discussions). When you do call it, is it expected before or after you code?

This could have consequences depending on what super's implementation does. Though you shouldn't have to know super's implementation to write yours.

Of course this goes for all of UIViewControllers delegate methods (willAppear, didAppear, etc...)

Any thoughts?

解决方案

My rule of thumb is: if you're doing something related to initialization, always call the super class's method first (if you are going to call it at all). This gives the super class a chance to do any set-up that you may be relying on later in your method. If you're doing something related to destruction, call the super class's method last. This makes sure that you can rely on the state of the object throughout the execution of your method. Finally, take any other case on a case-by-case basis. For instance, if you're handling an event, you probably want to deal with the event first, and only invoke the super class's method if you chose not to handle the event or if you somehow altered it and want to pass it along the event chain.

这篇关于`[super viewDidLoad]`约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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