Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does [super viewWillAppear] do, and when is it required?

-(void)viewWillAppear:(BOOL)animated{ //something here [super viewWillAppear]; } 

Is [super viewWillAppear]; always required? If not when and why do you use it?

like image 842
Yazzmi Avatar asked Jul 27 '10 21:07

Yazzmi


People also ask

What does viewWillAppear do?

The viewWillAppear method is called when the presentation of the view in the view hierarchy is about to start. Notably, this is called at the start of the animation (if any) of the presentation of the view. Its companion, viewWillDisappear is obviously called when the transition away from this view begins.

Should I call super viewDidAppear?

Not calling [super viewDidAppear] is indeed a bad idea. The base class for UIViewController has code in viewDidAppear that needs to be called or things won't work correctly.


1 Answers

First of all, the correct boiler plate should be:

-(void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    //something here } 

In other words, call super first, then do your own thing. And you have to pass the animated parameter to super.

You usually want to call the super class' implementation first in any method. In many languages it's required. In Objective-C it's not, but you can easily run into trouble if you don't put it at the top of your method. (That said, I sometimes break this pattern.)

Is calling super's implementation required? In the case of this particular method you could get unexpected behavior if you don't call it (especially if you have subclassed a UINavigationController for example). So the answer is no not in a technical sense, but you should probably always do it.

However, in many other methods there may be good reasons for not calling super.

like image 59
Felixyz Avatar answered Sep 19 '22 13:09

Felixyz