ios - 调用【super loadView】可以吗?

标签 ios uiviewcontroller loadview

我认为我永远不应该调用 [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,loadViewUIViewController 实现也会尝试加载其中包含关联类名的任何 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/

相关文章:

ios - 如何在用 TableView 加载另一个 TableView 时显示进度条

ios - loadNibNamed 从 Storyboard

iphone - 在 UITableViewController 子类中重写 loadView

ios - 在 Objective-C 中将自定义类注入(inject)子类继承树

ios - 主题不会保存在 Firebase 中

ios - UIView 出现错误的中心位置

ios - 在执行 unwindSegue 之前检查 UIViewController 是否已初始化

iphone - iOS 6.0 中的 CADisplayLink 不保留目标

ios - Google maps turn by turn 用于 ios 开发的 gps 导航

Swift Sprite Kit 应用内购买