I have noticed code from other sources where the author has not added the messages to super that are shown below. I usually add them both, but I was just curious as to what
I would recommend always send UIViewController
delegate messages to super
(e.g. viewDidLoad
, viewDidAppear
). In some cases, it is unnecessary, if you are subclassing UIViewController
directly for example.
Some classes, e.g. UITableViewController
, require subclasses to do that, as it is documented: "You may override loadView or any other superclass method, but if you do be sure to invoke the superclass implementation of the method, usually as the first method call."
The call to super
gives the super class a chance to handle the event (e.g. reload the table data, animate the de-selection of the button, etc).
Needless to say, you don't need an explicit method whose sole job is calling super.
You should only override a method when you need to perform some kind of action, so I'm assuming your code really looks something like this:
-(void)viewDidLoad {
//do things that need to be done when the view is loaded
[super viewDidLoad];
}
If you think the parent if your view needs to do something as well, then a call to [super viewDidLoad]; is definitely in order. If you look at the documentation for viewDidLoad, however, it states
This method is most commonly used to perform additional initialization steps on views that are loaded from nib files
The word "additional" here is crucial: it's just a convenience method. This is in contrast to dealloc, where the documentation states explicitly that when overriding dealloc you must always finish with a call to [super dealloc].
If your implementation of viewDidLoad and viewDidUnload really don't do anything else but call super, you should remove them: they accomplish nothing apart from making extra work for the compiler.
The purpose of those methods is to notify your controller when the view it controls has finished loading, and finished unloading, in case there are things that the controller needs to accomplish in those "interesting moments".
The code that you've included basically does nothing: it passes the messages on to the superclass implementation, without adding any processing of its own. But that's exactly what would happen if that code was omitted entirely: the message would be delivered to the lowest class in the inheritance chain that defined an implementation for the method.
You don't have to override these methods with empty implementation. Override it when you need to do something with ivars, stop notification observing, etc. Your code does nothing useful