0

What happens if you don't call super in your implementation of the view events (viewWillAppear, viewDidAppear etc.) of UIViewController?

It seems like I've forgotten to do this before, and it's unclear to me that there was any adverse impacts.

drc
  • 1,905
  • 4
  • 22
  • 28

2 Answers2

1

If you've subclassed your UIViewController delegate from anywhere (and you always subclass UIViewController at least once, in making your customized view controller), then any delegate methods in subclasses you've derived from won't get called.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Sorry can you clarify? Let's walk through an example to help me understand. Here is what I think you mean, is this right? I create a new viewController to control the home screen of the app - viewControllerA. In this, I override the viewWillAppear delegate method but don't call [super]. Then I subclass viewControllerA to create viewControllerB. viewControllerB will not receive a viewWillAppear delegate call. – drc Apr 28 '13 at 22:09
1

If you subclass one of your own view controller classes, you will certainly want to call super for any of these types of methods or your own base class's methods will not be invoked.

There's also the question of whether your top-level view controller classes need to call super to run the code in the base UIViewController class itself. In the UIViewController reference, it appears that the requirement to call super is documented for certain methods, among them viewWillAppear:, viewDidAppear, viewWillDisappear:, and viewDidDisappear:

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

However, there is no indication of what will happen if you fail to do so.

So apparently, there is something implemented in these methods in the base iOS framework view controller classes. Or at least, Apple reserves the option to implement something in these methods. You could say that they are virtual rather than abstract methods.

Mike Mertsock
  • 11,825
  • 7
  • 42
  • 75
  • Actually, I found that this has been answered before on StackOverflow; the answers to [this question](http://stackoverflow.com/q/5632511/795339) provide some additional detail and suggestions. – Mike Mertsock Apr 28 '13 at 22:17
  • Great answer. Thanks! I'm still curious to what is actually implemented in those base iOS methods, but this helped clear things up to the point of actually discussing that. – drc Apr 28 '13 at 22:17