[Go] loading ViewController -- solving the problem of viewDidLoad being loaded in advance (executed before pushViewController)

Source: Internet
Author: User

A ViewController is usually loaded through init or initWithNibName. There is no difference between the two. init still needs to call the initWithNibName method (unless the ViewController does not have a nib file ).

We often initialize views, variables, or other members in the initWithNibName method. This is the most common initWithNibName method:

-(Id) initWithNibName :( NSString *) nibNameOrNilbundle :( NSBundle *) nibBundleOrNil

{

Self = [superinitWithNibName: nibNameOrNil bundle: nibBundleOrNil];

If (self ){

Label = [[UILabelalloc] initWithFrame:

CGRectMake (0,0, 160,160)];

[Self. viewaddSubview: label];

...

    }

Returnself;

}

The if statement contains the most common member initialization code.

In this code, if you add some new UIView subclasses to the ViewController view tree, such as the code above:

[Self. viewaddSubview: label];

This is not a problem. However, this will lead to the call of another method, namely the viewDidLoad method.

The viewDidLoad method is generally called only after the nib file is loaded into the memory (that is, the view tree is built.

However, in another case, if the view attribute of ViewController is referenced, view = nil will also lead to the loading behavior of nib, which also leads to the call of viewDidLoad method. If you do not reference the view attribute in the initWithNibName method, the viewDidLoad method will not be triggered until the initWithNibName method ends.

You may be wondering, what if you never reference the view attribute of this ViewController in the code? Is the viewDidLoad method never called?

It is called when the ViewController object is present for 1st times. For example, if you use the presentModalViewController or pushViewController method, it is displayed. The two methods also call the appear method of ViewController (namely, the viewWillAppear method and the viewDidAppear method ).

The viewDidLoad method is executed earlier than the appear method (the appear method causes an animation to be generated ). If the viewDidLoad method has been executed before the present method, the present method does not trigger the viewDidLoad method.

This is why we wonder that code in the viewDidLoad method sometimes does not seem to be executed. The root cause is the if statement of the initWithNibName method.

If you reference the view attribute of ViewController in initWithNibName, because the view is nil at this time, the loading behavior of the nib file will be triggered, and the viewDidLoad method will be called in advance without waiting for the present. Because the ViewController member in the initWithNibName method has not been initialized, any reference to these members is invalid.

For example, in the viewDidLoad method, because the method is executed in advance, the Data Access Object is still nil (initWithNibName is still not executed ). If you want to obtain table data through the Data Access Object in the viewDidLoad method, it will be blank. On the surface, the viewDidLoad method does not seem to be executed.

There are two ways to improve this situation:

1. Do not include any code initialized by members in the initWithNibName method. Move the code to the viewDidLoad method to start.

II. Since the initWithNibName method is ensured to be performed in the present method, we can also retain the member initialization code in the initWithNibName method. However, the code in the original viewDidLoad method is moved to the appear method. That is, it is recommended that you do not initialize a member in the viewDidLoad method. In this way, the code in the appear method will be executed every time the presentViewController is used (if it is the viewDidLoad method, it will only be executed when the nib is loaded ).


Loadview is also called 1st times when ViewController's view attribute is referenced (in this case, view = nil), but before viewDidLoad. The difference is that loadview is called before the view tree is built, and viewDidLoad is called After The view tree is built. And then load the nib. Therefore, we often reload the loadview method when creating viewcontroller in programming mode, because nib content is not required.

 

Turn: http://blog.sina.com.cn/s/blog_947c4a9f01015i23.html

 

[Go] loading ViewController -- solving the problem of viewDidLoad being loaded in advance (executed before pushViewController)

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.