Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct sequent for calling [super viewWillAppear] [super viewDidLoad] etc.?

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?

i.e.

should it be:

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

or

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

etc.

like image 995
Gruntcakes Avatar asked Feb 10 '12 21:02

Gruntcakes


1 Answers

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".

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.

like image 144
Prometheus Avatar answered Oct 07 '22 15:10

Prometheus