简体   繁体   English

即使我们不像其他 ViewController 生命周期方法那样在 viewcontroller 中覆盖它,是否也会调用 loadView?

[英]does loadView get called even if we don't override it in viewcontroller like the other ViewController lifecycle methods?

Does loadView get called even if we don't override it in view controller like the other ViewController lifecycle methods?即使我们没有像其他 ViewController 生命周期方法一样在视图 controller 中覆盖它,是否也会调用 loadView? Even if we don't override the ViewDidLoad method, we know this lifecycle method will get called internally by ios.即使我们不重写 ViewDidLoad 方法,我们也知道这个生命周期方法将在 ios 内部调用。 Does the same thing happen for LoadView also? LoadView 也会发生同样的事情吗? or does it gets called only when the view inside the VC is nil or when we explicitly override it?还是仅在 VC 中的视图为 nil 或我们显式覆盖它时才调用它?

It always gets called.它总是被调用。 From the documentation ,文档中,

The view controller calls this method when its view property is requested but is currently nil.视图 controller 在其视图属性被请求但当前为 nil 时调用此方法。 This method loads or creates a view and assigns it to the view property.此方法加载或创建视图并将其分配给视图属性。

If the view controller has an associated nib file, this method loads the view from the nib file.如果视图 controller 具有关联的 nib 文件,则此方法从 nib 文件加载视图。 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 init(nibName:bundle:) method, or if iOS finds a nib file in the app bundle with a name based on the view controller'€™s class name. 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 init(nibName:bundle:) method,或者如果 iOS 在应用程序包中找到一个 nib 文件,其名称基于视图控制器的 class 名称。 If the view controller does not have an associated nib file, this method creates a plain UIView object instead.如果视图 controller 没有关联的 nib 文件,则此方法将创建一个普通的 UIView object 代替。

... ...

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.您创建的视图应该是唯一的实例,并且不应与任何其他视图 controller object 共享。 Your custom implementation of this method should not call super.此方法的自定义实现不应调用 super。

It gets called even if you don't override it.即使您不覆盖它,它也会被调用。 Generally, you would only override it when you don't want to create a view controller from its nib.通常,仅当您不想从其 nib 创建视图 controller 时才会覆盖它。 In this method, you would assign self.view some value (since view is loaded lazily).在此方法中,您将为self.view分配一些值(因为view是延迟加载的)。 If you're not assigning some special subclass to your view property, you can usually get by by adding all your logic to viewDidLoad() .如果您没有为您的视图属性分配一些特殊的子类,您通常可以通过将所有逻辑添加到viewDidLoad()来获得。

Here's a sample implementation:这是一个示例实现:

// Say you have some custom view you want use as your controller's view
class CustomView: UIView { ... } 

...
// In your view controller, you can set that custom instance to your view property.
override func loadView() {
    self.view = CustomView()
}

UITableViewController , for example, sets a UITableView as your view (presumably) in this method.例如, UITableViewController在此方法UITableView设置为您的视图(大概)。

By default, the view is just a vanilla UIView.默认情况下,视图只是一个普通的 UIView。 If that's all you need, there's no reason to call this method at all.如果这就是您所需要的,那么根本没有理由调用此方法。 viewDidLoad() is still a perfectly good place to do any additional initialization. viewDidLoad()仍然是进行任何额外初始化的绝佳场所。

A couple of things to remember:有几件事要记住:

  1. only assign to your view in loadView() .仅在loadView()中分配给您的视图。 Don't invoke it (on the right-hand side; don't invoke its getter), because that can cause an infinite loop.不要调用它(在右侧;不要调用它的 getter),因为这会导致无限循环。 If view is nil, loadView gets called to create it.如果view为 nil,则调用loadView来创建它。
  2. Don't call super.loadView() .不要调用super.loadView() This method is meant to assign a view to your view property.此方法旨在将视图分配给您的视图属性。 By calling super, you'd be doing this twice.通过调用 super,您将执行此操作两次。

A little more info on the infinite loop trap you could fall into:有关您可能陷入的无限循环陷阱的更多信息:

From UIViewController's view :从 UIViewController 的角度来看

If you access this property and its value is currently nil, the view controller automatically calls the loadView() method and returns the resulting view.如果您访问此属性并且其值当前为 nil,则视图 controller 会自动调用 loadView() 方法并返回结果视图。

view is created and assigned in loadView() when it is nil.view为 nil 时,在loadView()中创建并分配视图。 If you do that within loadView itself, you will prompt loadView to be called within its own body.如果您在loadView本身中执行此操作,您将提示loadView在其自身的主体中被调用。

Yes, it will still get called.是的,它仍然会被调用。 You'll observe the same behavior as if you were to add this to your view controller.您将观察到与将其添加到视图 controller 相同的行为。

override func loadView() {
  // Only call this to observe this is being called.
  // If you want to write your own implementation, you shouldn't call super.loadView().
  // Instead, create your own UIView instance and set self.view = yourView.
  super.loadView() 
}

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

相关问题 viewController生命周期 - viewController lifecycle 当调用navigationController popViewControllerAnimated时,不会调用viewController的dealloc - viewController's dealloc don't be called when called navigationController popViewControllerAnimated ViewController不启动 - ViewController don't launch 运行时错误消息:loadView 实例化视图 controller,标识符 ViewController 来自 storyboard Main,但没有获得 TableView - Runtime Error message: loadView instantiated view controller with identifier ViewController from storyboard Main,but didn't get a TableView 自定义模式解雇过渡后,不会调用第一个视图控制器的生命周期方法 - After custom modal dismissal transition the first view controller's lifecycle methods don't get called 不再显示ViewController - don't show again ViewController ZBarSDK不要关闭viewController - ZBarSDK don't dismiss the viewController 委托没有从另一个ViewController调用,尝试了一切 - Delegate doesn't get called from another viewcontroller, tried everything viewController中的UiTableView-未调用委托和dataSource方法 - UiTableView in viewController - delegate and dataSource methods not getting called 我们可以呈现像UIAlertController这样的半透明ViewController吗? - Can we present a translucence ViewController like UIAlertController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM