Differences between loadView, viewDidLoad, and viewDidUnload

Source: Internet
Author: User

I. loadView

When you do not use xib to create a view, there are two situations. 1. If the method is not reloaded in the implementation file, the default operation of this method is to create a UIView for the current VC view. Second, to recreate this method, you must create a UIView for the current VC in this method, and do not call super when rewriting this function. Some controls can be loaded in this function, but it is not recommended to add them here.

- (void)loadView{    // If you create your views manually, you MUST override this method and use it to create your views.    // If you use Interface Builder to create your views, then you must NOT override this method.        UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];    [view setBackgroundColor:[UIColor whiteColor]];    self.view = view;    [view release];        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 100, 40)];    label.backgroundColor = [UIColor redColor];    [self.view addSubview:label];    [label release];    }

Ii. viewDidload

This function is called whether it is xib or loadview. In most cases, perform subsequent initialization of xib.

3. viewDidUnload

This function is opposite to viewDidload. When the program memory is insufficient, this function is called by the controller. Since the controller usually stores the reference of view-related objects or other objects created at runtime, you must use this function to discard the ownership of these objects for memory reclaim. But do not release data that is hard to reconstruct.

Summary:

1. You can directly load the control in viewDidload without overwriting loadview, whether created using xib or pure code. The pure code only needs to use the init function during initialization.

2. (loadView/nib file) to load the view to the memory --> viewDidLoad function to further initialize these views --> when the memory is insufficient, call the viewDidUnload function to release views

--> When you need to use a view, the first step is returned to this alternating loop.

Reference: http://www.dreamingwish.com/dream-2011/correct-online-information-error-loadview-viewdidload-viewdidunload.html

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.