问题描述
我以为我永远都不要打电话给[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.
我假设这是为了避免从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:
还请注意,即使在分配UIViewController
对象的过程中,您为nbNameOrNil参数传递了nil,loadView
的UIViewController
实现也将尝试加载其中包含相关类名的任何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.
此方法的真正目的是让您完全控制构建视图层次结构,而无需依赖内置的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.:
个人而言,如果满足以下条件,我将覆盖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]吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!