Detail the loadView method of UIViewController used in iOS App development

  • 2020-05-17 06:36:20
  • OfStack

When you access the view property of an ViewController, if the value of view is nil, then ViewController will automatically call the loadView method. This method loads or creates an view object and assigns it to the view property.
By default, loadView does this: if there is a corresponding nib file for ViewController, load nib. Otherwise, an UIView object is created.
If you are using Interface Builder to create an interface, you should not override this method.

The loadView method of the controller and the view properties
The controller has an view property, which we often access in the controller through self.view. This view is a very interesting thing.
The first thing to understand is what view really is.
When a controller becomes the root controller of window, when the interface is about to be displayed, the view of the controller will be added to window to display the interface. Normally the view that comes with the controller is "colorless and transparent".
If you create an Button directly onto window before setting the window root controller, button appears on window, but below the view of the controller. Click the button and you will find that the button does not respond to the click.
This is because the controller's view intercepts the click event, which indicates that it is not really completely transparent (because if it is completely transparent, then it will not intercept the click event, you can set viewController.view.alpha = 0 to verify, then the button can respond to the click). In fact, view is a view with backgroundColor as clearColor, and clearColor is an almost transparent color but not completely transparent. That is to say, view in clearColor background will intercept the click event when there is a similar click event, because it is not completely transparent.
So we can say that the view of the controller is an invisible thing...
In addition, view is lazily loaded, that is, it will only be created when view of the controller is actually used. It is created in the controller's -loadView method.


- (void)loadView
{
      // Something like this
      // There may be more to do, such as determining if there is a specification storyboard If so, it will load storyboard Medium controller view Operations such as
    UIView *view = [[UIView alloc] init];
      view.backgroundColor = [UIColor clearColor];
      // ...
      _view = view;
}

As mentioned earlier, view is lazily loaded, so when using self.view:


- (UIView *)view
{
      // Something like this
    if (_view == nil) {
        [self loadView];
        [self viewDidLoad];
    }
    return _view;
}

The loadView method method is called when _view is nil, and by default the system creates an UIView object and assigns it to _view, where _view has a value, and then calls the viewDidLoad method.
When you visit self.view again, because _view already has a value, it will return _view directly. This is why the loadView and viewDidLoad methods are only executed once.
As mentioned in the previous article, after setting up the root controller for window, the view of the root controller is not immediately added to window, that is, view is not yet created. After [self.window makeKeyAndVisible] is displayed, view is used, view's getter method is called, loadView is executed, and viewDidLoad and other 1 series methods are executed.
Now we know that view is lazily loaded, so if the view of the controller is used before [self.window makeKeyAndVisible], it will be created "ahead of time".


NSLog(@"%@", viewController.view); // At this time to use view, Will be created view

Now that we know that the view of the controller was created using the loadView method, we can override this method to change the view of the controller:


- (void)loadView
{
    JYView *view = [[JYView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    view.background = [UIColor blueColor];
    _view = view;
}

At this point, we have changed the view that the controller created automatically, and now we use self.view to get the object of JYView.


Some notes on the use of loadView
Never call this function actively. view controller will call this function when property of view is requested and the current view value is nil. If you manually create view, you should override this function. If you create view with IB and initialize view controller, that means you are using initWithNibName:bundle: method, and you should not overload loadView functions.
The default implementation of this method looks for information about the available nib file and loads the nib file based on this information. If there is no information about the nib file, the default implementation creates a blank UIView object and then makes this object the master view of controller.
So, when you overload this function, you should do the same. Assign view to view (property) (your create view must be a unique instance of controller and not Shared by any other controller), and the function you overloaded should not call super.
If you are going to go ahead and initialize your views, you should do it in the viewDidLoad function. In iOS 3.0 and later, you should override the viewDidUnload function to release any reference to view or its contents (child view, etc.).


Related articles: