Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should [super loadView] be called from loadView or not?

Tags:

ios

In Programming iOS 4 by Matt Newburg he states:

“To provide a UIViewController with a view manually, implement its loadView method… You must NOT call super”.

In the iOS 5 Developer's Cookbook by Erica Sadun she states:

“The loadView method allows you to set up the screen and layout any subviews… Make sure to call [super loadView] whenever you inherit from a specialized subclass such as UITableViewController or UITabBarController.”

This, to me at least, is confusing.

like image 866
Gruntcakes Avatar asked Feb 02 '12 00:02

Gruntcakes


People also ask

When loadView is called?

The view controller calls this method when its view property is requested but is currently nil . This method loads or creates a view and assigns it to the view property. If the view controller has an associated nib file, this method loads the view from the nib file.

Should I call super loadView?

The loadView method can be used to create your own views manually. You should never call this method directly, but it's save to override it. The other thing that you should not is that if you are using this method to override the root view, then you shouldn't call super. loadView() .


2 Answers

Apple is the source of truth and they say NO super call.

If you override this method in order to create your views manually, you should do so and assign the root view of your 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.

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621454-loadview

[edit]

Another important note scattered around in the UIViewController class reference:

The default loadView method attempts to load the view from the nib file associated with the view controller (if any).

like image 77
Rog Avatar answered Oct 25 '22 18:10

Rog


This is a very old question, but I find that it needs a better answer than the one it got.

Should [super loadView] be called from loadView or not?

It depends. The two sources you cite are talking about different situations, and they're both correct in the context they're describing.

The quote from Neuberg is talking about view controllers that inherit directly from UIViewController. That class has its own implementation of loadView that provides default behavior; specifically, it automatically loads the view hierarchy from a .xib (or storyboard) file that's associated with the view controller. If you call UIViewController's version of that method, the view hierarchy created in that method will either replace your own implementation's view hierarchy, or vice versa. Nine years after this question was posed, the documentation for UIViewController's -loadView method still warns against that:

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. [emphasis added]

The quote from Sadun is talking about a different situation, i.e. one in which your view controller is not a direct subclass of UIViewController, but is instead derived from UITableViewController, UITabBarController, etc. Those classes override -loadView themselves and need their versions called. At least in the case of UITableViewController, this is called out in the Overview section:

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.

So, if you're subclassing UIViewController and providing your own -loadView implementation to generate the controller's views rather than using a .xib or storyboard to provide the views, don't call the superclass's -loadView method. On the other hand, if you're subclassing a class such as UITableView and doing the same thing, check the docs to see whether you need to call that class's -loadView method from your own override.

like image 37
Caleb Avatar answered Oct 25 '22 17:10

Caleb