IOS Viewdidload method is executed two times (Viewdidload and Loadview methods in detail)

Source: Internet
Author: User

In order to understand the viewdidload, Loadview and other methods of use and call order, wrote a small demo, Behold Viewdidload method was executed two times, the simulator has been a black screen. After reviewing the official explanations for these two methods, we found the cause and resolution of the error.

First look at the explanation of the Viewdidload method in the UIViewController.h file:

afterForinisafterForisafteris set.

The following is an explanation of the Loadview method:

create their custom view hierarchy if they aren‘t using a nib. Should never be called directly.

Whether you load a view from a xib file or create a view yourself, the Viewdidload method is called and is called after the view has been loaded, so it is clear that any UI control creation should not be implemented in the Viewdidload method. Because this is the task of the Xib file or the Loadview method. Viewdidload is just a one-week extension and supplemental mechanism for loading views through the nib or Loadview method, such as some properties that are not easily set through the nib or the content of the control that needs to be processed temporarily.

As for the relationship between the Loadview method and the Xib file, the personal understanding is that the creation of a real UI control is done by invoking the code in Loadview, except that in the Xib file, the code is saved in an XML-formatted document. And Apple parses the XML document through storyboard, creating a visual graphical interface. This is the common denominator of the two, and as for the differences, the explanation in the official documentation is clear, and once the Loadview method is called it means that it is not using a nib file. This means that the Loadview and xib files are incompatible. The Loadview has a higher priority.

Here are a few experiments to prove the above point.
Lab 1:
Create an empty project file and write a simple line of code in the Viewdidload function:

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

Experimental results: After running the program found that the console output a row of results "123". The simulator displays a pure white interface.
Experimental Analysis: The results of the console are very easy to understand. But we didn't create a uiview or set its background color, how did the system create what we saw in the simulator?
The answer is that if we do not implement the Loadview method, the system will default to creating the view in a custom way instead of loading it from the nib file. Just go back to storyboard or the corresponding nib file to find the code that loads the view. Because the default Viewcontroller class has its own nib file in Main.storyboard, a uiview can be loaded successfully.

Lab 2:
To implement an empty Loadview method:

- (void)loadView{}

Experimental results: After running the program found that the console output two lines "123", 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 would like to customize a UIView object, so as to execute the code in the Loadview method, try to create a uiview, but obviously this will fail, So get a black (no content) interface. As for why the Viewdidload method will be called two times, the reason is not clear (want to read here, Daniel Message).

Experiment Three:
Implement the complete Loadview method:

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

Experimental results: The results of the same experiment 1
Experimental Analysis: Implemented a complete UIView creation process, very similar to the one created by nib file.

The following conclusions are drawn from the above experiments:
1. If you want to create UIView through custom code, write the code into the Loadview method.
2. If you want to create UIView with the nib file, do not override the Loadview method.
The 3.viewDidLoad method is executed twice most likely to be caused by rewriting an incomplete Loadview method.

IOS Viewdidload method is executed two times (Viewdidload and Loadview methods in detail)

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.