I thought that I should never call [super loadView] but something is confusing me.
In description of loadView (UIViewController Class Reference) it is said that "Your custom implementation of this method should not call super.", but in ZoomingPDFViewer example that they gave, loadView implementation (ZoomingPDFViewerViewController) is calling [super loadView].
I have tried to call it from my loadView method and it works ok, but I just don't understand then what does it mean to not call super.
You definitely should not be calling
[super loadView]
. I'd say you found a bug in the ZoomingPDFViewer example.You override
loadView
when you want to programatically create the view hierarchy for your view controller (not using a xib).As you pointed out, the docs clearly state that you should not call super.
I assume this is to avoid loading both from a xib and programatically creating a view as this method is used by the base to load a view from a xib:
Note also that even if during allocation of your
UIViewController
object you pass nil for the nibNameOrNil parameter that theUIViewController
implementation ofloadView
will try to load any xib with the associated class name in it.The real intent of this method is to give you full control of building the view hierarchy without relying on the built in xib loading mechanism.:
Personally, I override
loadView
if: 1.) The xib I would make for it is really trivial or 2.) The layout of the control is very dynamic, so creating a xib with a static layout has little benefit.If you dont have a view created in your IB, then you should call [super loadView] in your code to give a view to your program.
In case of your custom views, you are suppose to create a view with the interfaz builder, so you dont need to call it.
If you create your ViewController programmatically, you can call
super.loadView()
instead ofself.view = UIView(frame: UIScreen.main.bounds)
at the beginning ofoverride func loadView()
to build your view hierachy. However, you should not callsuper.loadView()
and create your customUIViewController.view
at the same time. Just do not like a robot to explain Apple's documentation.NSViewController tries to initialize a view from a nib in -loadView. Since your nib name is not set for your controller, it will just give you a self.view = nil; I would assume UIViewController works the same way.
So it should be safe, but you it's completely unnecessary.