LoadView usage summary and loadView usage Summary

Source: Internet
Author: User

LoadView usage summary and loadView usage Summary
I,LoadView1.When Will loadView be called?

The loadView method is called every time you access the view (such as controller. view and self. view) of UIViewController and the view is nil.

2. What is the function?

The loadView method is used to create a view of UIViewController.

3. What is the default implementation?

By default, [super loadView] has done many things:

               1> it first looks for the xib file associated with UIViewController and creates the view of UIViewController by loading the xib file.

* If the xib file name is specified in the initialization UIViewController, the corresponding xib file [[MJViewController alloc] initWithNibName: @ "MJViewController" bundle: nil] will be loaded Based on the imported xib file name; * If the xib file name is not explicitly uploaded, The xib file [[MJViewController alloc] init] with the same name as UIViewController will be loaded; // The MJViewController will be loaded. xib 2> If the associated xib file is not found, a blank UIView is created and assigned to the view attribute of UIViewController, which is roughly as follows: self. view = [[[UIView alloc] initWithFrame: [UIScreen mainScreen]. applicationFrame] autorelea Se]; // The applicationFrame value is: {x = 0, y = 20}, {width = 320, height = 460} [super loadView] roughly completes the content described in 1> and 2> 4. how can I use this method correctly?

UIViewController can be created through the xib file, but in some cases, xib is not so flexible, and sometimes we want to create UIView through code.     

For example, self. view = [[UIWebView alloc] initWithFrame: [UIScreen mainScreen]. applicationFrame] autorelscreen];

If you want to use code to create a view of UIViewController, You need to override the loadView method and do not need to call [super loadView]. Point 3 has mentioned that if there is no xib file,

[Super loadView] A blank UIView is created by default. Since we need to customize the UIVIew through code, there is no need to create a blank UIView in advance to save unnecessary overhead. The correct method should be as follows:

-(Void) loadView {

Self. view = [[[UIWebView alloc] initWithFrame: [UIScreen mainScreen]. applicationFrame] autorelease];} does not need to call [super loadView]. Even if it is called, no errors will occur, but unnecessary overhead will be incurred. All in all, Apple designed this method for customizing the view of UIViewController. Ii. viewDidLoad 1. When will it be called?

The viewDidLoad method is called after the view is created through the xib file or by overwriting the view of the UIViewController created by the loadView method.

2. What is the function?

Initialization on the interface is generally performed here, such as adding subviews to the view, loading model data from the database or network, and assembling it into the subview.

For example:

          -(Void) viewDidLoad {

[Super viewDidLoad]; // Add a button UIButton * button = [UIButton buttonWithType: Custom]; [button addTarget: self action: @ selector (click) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: button];} 3. When will viewDidUnload1. be called? The memory of iOS devices is extremely limited. If the application occupies too much memory, the system will issue a memory warning to the application. UIViewController receives the didReceiveMemoryWarning message. The default implementation of the didReceiveMemoryWarning method is: if the view of the UIViewController is not in the View Hierarchy of the application, that is, when the superview of the view is nil, the view is released, and call the viewDidUnload method 2. what is the function? As mentioned above, the viewDidUnload method is called when a memory warning is issued and the view is released. Therefore, resources related to interface elements are usually released, assign all related instances to nil-(void) viewDidUnload {[super viewDidUnload]; self. name = nil; self. pwd = nil;} 3. dealloc is also used to release resources. What is the relationship with viewDidUnload? When the viewDidUnload method is called with a memory warning, the view is released and the UIViewController is not released. Therefore, the dealloc method is not called. That is, the viewDidUnload and dealloc methods are irrelevant. The dealloc method is called only when the UIViewController is released. Iv. Relationship between the three methods 1. When you access the view of UIViewController for the first time, the view is nil and the loadview method is called to create the view. 2. After the view is created, call the viewDidLoad method to initialize the interface elements. 3. When the memory is warned, the system may release the UIViewController, assign the view value to nil, and call the viewDidUnload method. 4. When you access the view of UIViewController again, the view has been assigned as nil in 3, so you have to call the loadview method to recreate the view. 5. After the view is re-created, the viewDidLoad method is called to initialize the element.

 

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.