可以调用[super loadView]吗? [英] Is it ok to call [super loadView]?

查看:190
本文介绍了可以调用[super loadView]吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为我永远都不要打电话给[super loadView],但是有些事情让我感到困惑.

I thought that I should never call [super loadView] but something is confusing me.

在loadView的描述中( UIViewController类参考文献),据说您对此方法的自定义实现不应调用super.", 但是在他们给出的ZoomingPDFViewer示例中,loadView实现( ZoomingPDFViewerViewController )正在调用[super loadView].

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

我试图从我的loadView方法中调用它,并且它可以正常工作,但是我不明白那么不调用super意味着什么.

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.

推荐答案

您绝对不应该呼叫[super loadView].我想说您在ZoomingPDFViewer示例中发现了一个错误.

You definitely should not be calling [super loadView]. I'd say you found a bug in the ZoomingPDFViewer example.

要以编程方式为视图控制器(不使用xib)创建视图层次结构时,请覆盖loadView.

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.

您对此方法的自定义实现不应调用super.

Your custom implementation of this method should not call super.

我假设这是为了避免从xib加载并以编程方式创建视图,因为基础使用此方法从xib加载视图:

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:

如果视图控制器具有关联的nib文件,则此方法将加载 nib文件中的视图.

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

还请注意,即使在分配UIViewController对象的过程中,您为nbNameOrNil参数传递了nil,loadViewUIViewController实现也将尝试加载其中包含相关类名的任何xib.

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.

如果nibName属性,则视图控制器具有关联的nib文件 返回非nil值,如果视图控制器为 从情节提要中实例化(如果您明确为其指定了笔尖) 使用initWithNibName:bundle:方法的文件,或iOS是否找到笔尖 应用套件中的文件,其名称基于视图控制器的 类名.如果视图控制器没有关联的笔尖 文件,则此方法将创建一个普通的UIView对象.

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.

此方法的真正目的是让您完全控制构建视图层次结构,而无需依赖内置的xib加载机制.

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.

个人而言,如果满足以下条件,我将覆盖loadView:1.)我为此而设计的xib确实很小,或者2.)控件的布局非常动态,因此创建具有静态布局的xib几乎没有好处.

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.

这篇关于可以调用[super loadView]吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆