Is it ok to call [super loadView]?
Asked Answered
A

4

28

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.

Approval answered 5/3, 2012 at 15:57 Comment(0)
N
32

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.

Your custom implementation of this method 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:

If the view controller has an associated nib file, this method loads the view from the nib file.

Note also that even if during allocation of your UIViewController object you pass nil for the nibNameOrNil parameter that the UIViewController implementation of loadView will try to load any xib with the associated class name in it.

A view controller has an associated nib file if the nibName property returns a non-nil value, which occurs if the view controller was instantiated from a storyboard, if you explicitly assigned it a nib file using the initWithNibName:bundle: method, or if iOS finds a nib file in the app bundle with a name based on the view controller’s class name. If the view controller does not have an associated nib file, this method creates a plain UIView object instead.

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.:

You can override this method in order to create your views manually.

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.

Neoterism answered 5/3, 2012 at 16:5 Comment(0)
P
1

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.

Penley answered 5/3, 2012 at 16:9 Comment(1)
While it should be harmless, if there was a xib with a name based on the view controller name, then calling [super loadView] would load that nib into self.view. I've never actually seen this happen (since I never call the super), but based on the docs that's what I'd expect. Also, I think calling [super loadView] would minimally assign a generic view to self.view, which is what your code should be doing. Again this won't hurt anything (assuming the custom implementation of loadView assigns a value to self.view after calling the super), but it is wasteful.Neoterism
I
0

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.

Ishmael answered 5/3, 2012 at 16:4 Comment(3)
Ok, but what does it mean "Your custom implementation of this method should not call super."?Gauntlett
it means that you are creating the view, so you dont need to make your super load itIshmael
You give a view to your program by doing something like: UIView *myView = [[[UIView alloc] init] autorelease]; ... (create view hierarchy) ... self.view = myView; If you aren't assigning self.view to a new value, then what's the point of overriding loadView to begin with?Neoterism
M
0

If you create your ViewController programmatically, you could call super.loadView() instead of self.view = UIView(frame: UIScreen.main.bounds) at the beginning of override func loadView().

However, do NOT call self.view before super.loadView(), since the former will trigger the latter if view did not been loaded.

I don't think it's a good idea to explain Apple's documentation like a robot.

Mathison answered 26/7, 2017 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.