简体   繁体   English

[super loadView] 是否应该从 loadView 调用?

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

In Programming iOS 4 by Matt Newburg he states:在 Matt Newburg 的 Programming iOS 4 中,他说:

“To provide a UIViewController with a view manually, implement its loadView method… You must NOT call super ”. “要手动为UIViewController提供视图,请实现其loadView方法……您不能调用super ”。

In the iOS 5 Developer's Cookbook by Erica Sadun she states:在 Erica Sadun 的 iOS 5 Developer's Cookbook 中,她指出:

“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 .” loadView方法允许你设置屏幕和布局任何子视图......每当你从一个专门的子类(如UITableViewControllerUITabBarController继承时,请确保调用[super loadView] 。”

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. (您创建的视图应该是唯一的实例,不应与任何其他视图控制器对象共享。)此方法的自定义实现不应调用 super。

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

[edit] [编辑]

Another important note scattered around in the UIViewController class reference:散布在 UIViewController 类参考中的另一个重要说明:

The default loadView method attempts to load the view from the nib file associated with the view controller (if any).默认的 loadView 方法尝试从与视图控制器(如果有)关联的 nib 文件加载视图。

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?是否应该从 loadView 调用[super loadView]

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 . Neuberg 的引用是在谈论直接从UIViewController继承的视图控制器。 That class has its own implementation of loadView that provides default behavior;该类有自己的loadView实现,提供默认行为; specifically, it automatically loads the view hierarchy from a .xib (or storyboard) file that's associated with the view controller.具体来说,它会自动从与视图控制器关联的 .xib(或故事板)文件加载视图层次结构。 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.如果您调用该方法的UIViewController版本,则在该方法中创建的视图层次结构将替换您自己实现的视图层次结构,反之亦然。 Nine years after this question was posed, the documentation for UIViewController 's -loadView method still warns against that:在提出这个问题九年后UIViewController-loadView方法的文档仍然警告说:

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.此方法的自定义实现不应调用 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.来自 Sadun 的引用是在谈论一种不同的情况,即您的视图控制器不是UIViewController的直接子类,而是派生自UITableViewControllerUITabBarController等。这些类覆盖-loadView本身并需要调用它们的版本。 At least in the case of UITableViewController , this is called out in the Overview section:至少在UITableViewController的情况下,这是在概述部分中UITableViewController

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.您可以覆盖 loadView 或任何其他超类方法,但如果这样做,请确保调用该方法的超类实现,通常是作为第一个方法调用。

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.因此,如果您正在-loadView UIViewController并提供您自己的-loadView实现来生成控制器的视图,而不是使用 .xib 或故事板来提供视图,请不要调用超类的-loadView方法。 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.另一方面,如果您对UITableView等类进行子类化并执行相同的操作,请检查文档以查看是否需要从您自己的覆盖中调用该类的-loadView方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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