简体   繁体   中英

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

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.

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

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 , ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM