调用 [super viewWillAppear] [super viewDidLoad] 等的正确顺序是什么? [英] What is the correct sequent for calling [super viewWillAppear] [super viewDidLoad] etc.?

查看:65
本文介绍了调用 [super viewWillAppear] [super viewDidLoad] 等的正确顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当提供 viewWillAppearviewDidLoadviewDidAppearloadView 等的实现时

When providing an implementation of viewWillAppear, viewDidLoad, viewDidAppear, loadView etc.

应该在执行自定义操作之前还是之后调用超类对应的方法?

Should the calls to the superclasses corresponding methods be made before or after performing custom action?

如果以错误的顺序执行会产生什么后果?

What are some possible consequences if performed in the wrong order?

应该是:

- (void)viewWillAppear:(BOOL)animated
{    
    [super viewWillAppear:animated];
    stuff
}

- (void)viewWillAppear:(BOOL)animated
{    
    stuff
    [super viewWillAppear:animated];
}

推荐答案

对于您想做的绝大多数事情,它根本没有任何区别.首先调用super"很方便,因为稍后很容易检查并确保您正在调用super.Apple 的文档只是指出您必须在实现中的某个时刻调用 super".

For the vast majority of things you'd like to do, it won't make any difference at all. It's convenient to place the call to "super" first, because then it's easy to check later an make sure you are calling super. Apple's documentation just states that "you must call super at some point in your implementation".

有一种情况可能更重要.如果您不是直接从 UIViewController 继承,而是从另一个自定义类继承,那么您应该在做出决定时研究该类的特定行为.一般而言,首先调用 super 是一种良好的设计模式,可以轻松地在调试时始终预测行为.

There is one case where it is more likely to matter. If you are not inheriting directly from UIViewController, but instead from another custom class, then you should research the specific behavior of that class in making your decision. In general, calling super first makes for a good design pattern to make it easy to always predict behavior when debugging.

这篇关于调用 [super viewWillAppear] [super viewDidLoad] 等的正确顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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