Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When exactly should I call [super viewWillAppear:] and when not?

I have always thought that calling [super viewWillAppear:animated] is mandatory. But today I came across a piece of code from Apple's example application called CoreDataBooks and here's viewWillAppear: implementation from there:

- (void)viewWillAppear
{
   [self.tableView reloadData];
}

Note there is no call to super. This confuses me a lot. If even Apple's code doesn't include call to [super viewWillAppear:] then maybe I can always omit it? Or maybe there are certain rules about this matter? Can anybody explain it?

like image 349
Andrey Chernukha Avatar asked Sep 28 '13 07:09

Andrey Chernukha


People also ask

When viewDidAppear is called?

1. 'viewDidAppear' will get called when the view is fully loaded (i.e after 'viewWillAppear'). It will not get called when you add/remove a sub view!! – Nina. Jul 18, 2012 at 4:52.

Which is called first viewDidLoad or viewWillAppear?

viewWillAppear(_:) Unlike viewDidLoad , viewWillAppear is called the first time the view is displayed as well as when the view is displayed again, so it can be called multiple times during the life of the view controller object.

What happens between viewWillAppear and viewDidAppear?

There is a noticeable pause after row selection and before the new view is pushed. Some logging indicates that all of my code is reasonably quick, from row selection until the pushed controller's viewWillAppear . But then the time between viewWillAppear and viewDidAppear is logged at around 0.7 seconds.

What is called before viewWillAppear?

ViewDidLoad is called when the view is loaded in to memory. i.e if you are using storyboard, the app has unarchived the view and loaded it into memory(not yet on screen). When the app is ready to load the view on the screen it will call the viewWillAppear method.


2 Answers

Even though not calling the super implementation can be harmless sometimes, you should ALWAYS call [super viewWillAppear:], regardless of the bad examples Apple publishes. Not doing it may lead to very nasty issues to track down.

According to the documentation

If you override this method, you must call super at some point in your implementation.

The specific example you mentioned is broken in a more subtle way: they implemented viewWillAppear instead of viewWillAppear:, so that piece of code is not even being executed!

like image 51
Gabriele Petronella Avatar answered Oct 06 '22 22:10

Gabriele Petronella


in short: always.

if you don't that can cause issues that are hard to track

like image 44
Daij-Djan Avatar answered Oct 06 '22 22:10

Daij-Djan