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?

有帮助吗?

解决方案

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!

其他提示

in short: always.

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top