super.loadView() Not Called in Override in Creating Map View

Why isn’t super.loadView() being called inside the override function when creating the map view?

import UIKit
import MapKit

class MapViewController: UIViewController {
    
    var mapView: MKMapView!
    
    override func loadView() {
        mapView = MKMapView()
        view = mapView
    }
.
.
.

The code in this chapter is creating views programmatically instead of using Interface Builder. The default implementation of loadView provided by Apple loads views created in Interface Builder, so you don’t want to call that when you’re not using IB. Apple even explicitly tells you in the loadView() documentation not to call it when creating views programmatically:

You can override this method in order to create your views manually. If you choose to do so, assign the root view of your view hierarchy to the view property. The views you create should be unique instances and should not be shared with any other view controller object. Your custom implementation of this method should not call super.

I suspect if you ignore this advice & call super.loadView() anyway, either it’s going to draw stuff on the screen you don’t want or it’s going to crash; that’s something you could verify on your own.

1 Like

Thank you. I did try it - it doesn’t crash and I’m not noticing any difference at all but perhaps the app is too small to hit a case where it matters.