Why/when do we have to call super.ViewDidLoad?

Solution 1:

Usually it's a good idea to call super for all functions you override that don't have a return value.

You don't know the implementation of viewDidLoad. UIViewController could be doing some important setup stuff there and not calling it would not give it the chance to run it's own viewDidLoad code.

Same thing goes when inheriting from a UIViewController subclass.

Even if calling super.viewDidLoad doesn't do anything, always calling it is a good habit to get into. If you get into the habit of not calling it, you might forget to call it when it's needed. For example when subclassing a ViewController that depends on it from a 3rd party framework or from your own code base.

Take this contrived example:

class PrintingViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        print("view has loaded")
    }
}

class UserViewController: PrintingViewController {
    override func viewDidLoad() {
    super.viewDidLoad()

     // do view setup here
    }

}

Not calling viewDidLoad here would never give PrintingViewController a chance to run its own viewDidLoad code

If you don't want to do anything in viewDidLoad just don't implement it. The super method will be called anyway.

Solution 2:

I have a secret, when I worked at Apple I read the source code for UIKit, partly to answer questions I had like this, viewDidLoad is empty in all the UI*ViewController classes.

Naturally I am not there anymore, they may have changed this.