The IOS viewDidLoad method is executed twice (detailed description of the viewDidLoad and loadView methods)

Source: Internet
Author: User

The IOS viewDidLoad method is executed twice (detailed description of the viewDidLoad and loadView methods)

To understand the usage and call sequence of methods such as viewDidLoad and loadView, a small demo is written. unexpectedly, the viewDidLoad method is executed twice, and the simulator has been a black screen. After checking the official explanations of the two methods, we found the cause of the error and the solution.

First, let's take a look at the explanation of the viewDidLoad method in the UIViewController. h file:

- (void)viewDidLoad; // Called after the view has been loaded. For view controllers created in code, this is after -loadView. For view controllers unarchived from a nib, this is after the view is set.

Next we will explain the loadView method:

- (void)loadView; // This is where subclasses should create their custom view hierarchy if they aren't using a nib. Should never be called directly.

The viewDidLoad method is called whether to load a view from an xib file or create a view by yourself. The viewDidLoad method is called after the view is loaded, the creation of any UI control should not be implemented in the viewDidLoad method, because it is a task of the xib file or loadView method. ViewDidLoad only uses the nib or loadView method to load the view's one-week extension and supplement mechanism. For example, some attributes that are not convenient to be set through nib or the control content that needs to be processed temporarily.

As for the relationship between the loadView method and the xib file, my personal understanding is: the creation of real UI controls must be implemented by calling the code in loadView, but in the xib file, the code is saved as an xml document, and Apple parses the xml document through the storyboard to form a visual graphical interface. This is the commonality between the two. As for the differences, the explanation in the official document is clear. Once the loadView method is called, it means that the nib file is not used. This means that loadView and xib files are not compatible. LoadView has a higher priority.

The above points are proved through several experiments below.
Lab 1:
Create an empty project file and write a simple line of code in the viewDidLoad function:

- (void)viewDidLoad {    [super viewDidLoad];    NSLog(@"123");}

Experiment results: after running the program, the console outputs a line of result "123 ". The simulator displays a pure white interface.
Experiment analysis: the console results are easy to understand. But we didn't create a UIView or set its background color. How did the system create the content we saw in the simulator?
The answer is: if we do not implement the loadView method, the system will not use the custom method to create the view by default, but load it from the nib file. At this time, I will go back to the storyboard or other corresponding nib files to find the code for loading the view. Because the default ViewController class has its own nib file in main. storyboard, you can successfully load a UIView.

Lab 2:
Implement an empty loadView method:

- (void)loadView{}

Experiment results: after running the program, the console outputs two lines: "123" and the simulator black screen.
Experimental analysis: the simulator black screen is very easy to understand, because we implemented the loadView method, the compiler thought we were going to customize a UIView object to execute the code in the loadView method and try to create a UIView, but obviously this will fail, so we get a black (NO content) interface. The reason why the viewDidLoad method is called twice is unclear (hope to read the comment here ).

Lab 3:
Complete loadView implementation method:

- (void)loadView{    self.view = [[ UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];    self.view.backgroundColor = [UIColor whiteColor];}

Experiment results: Same as the results of experiment 1
Experiment Analysis: implements a complete process of creating a UIView, which is similar to creating a nib file.

Through the above experiments, we can draw the following conclusions:
1. If you want to create a UIView using custom code, write the code into the loadView method.
2. If you want to create a UIView through the nib file, do not overwrite the loadView method.
3. If the viewDidLoad method is executed twice, it is likely that an incomplete loadView method is overwritten.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.