Junior — Middle
In what case is the loadView method called in the lifecycle of a controller?
sobes.tech AI
Answer from AI
The loadView method is called in the UIViewController lifecycle when the controller needs to load its root view, but it has not been loaded yet.
The system calls loadView in the following cases:
- When the
viewproperty of the controller is requested for the first time, and the view has not been loaded yet. - If you are not using a storyboard or xib, and want to create the view programmatically, you override
loadViewto create and assignself.view.
If loadView is not overridden, the controller attempts to load the view from a storyboard or xib by default. If none are available, a blank UIView is created.
Example of overriding:
override func loadView() {
let customView = UIView()
customView.backgroundColor = .white
self.view = customView
}
Thus, loadView is called once when view is first accessed, if the view has not been loaded yet.