我认为我永远不应该调用 [super loadView] 但有些事情让我感到困惑。
在 loadView ( UIViewController Class Reference ) 的描述中说“您对此方法的自定义实现不应调用 super。”, 但在他们给出的 ZoomingPDFViewer 示例中,loadView 实现 ( ZoomingPDFViewerViewController ) 正在调用 [super loadView]。
我尝试从我的 loadView 方法调用它并且它工作正常,但我只是不明白不调用 super 是什么意思。
最佳答案
您绝对不应该调用 [super loadView]
。我会说您在 ZoomingPDFViewer 示例中发现了一个错误。
当您想以编程方式为 View Controller (不使用 xib)创建 View 层次结构时,您可以覆盖 loadView
。
正如您所指出的,文档明确声明您不应该调用 super。
Your custom implementation of this method should not call super.
我认为这是为了避免同时从 xib 加载和以编程方式创建 View ,因为 base 使用此方法从 xib 加载 View :
If the view controller has an associated nib file, this method loads the view from the nib file.
另请注意,即使在分配 UIViewController
对象期间,您为 nibNameOrNil 参数传递了 nil,loadView
的 UIViewController
实现也会尝试加载其中包含关联类名的任何 xib。
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.
此方法的真正目的是让您完全控制构建 View 层次结构,而无需依赖内置的 xib 加载机制。
You can override this method in order to create your views manually.
就我个人而言,如果出现以下情况,我会重写 loadView
:1.) 我为它制作的 xib 非常简单,或者 2.) 控件的布局非常动态,因此创建一个带有静态的 xib布局没有什么好处。
关于ios - 调用【super loadView】可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9569749/