IOS loadview, viewdidload, and viewdidunload usage

Source: Internet
Author: User

Reposted from: www.dreamingwish.com


Loadview, viewdidload, and viewdidunload. How should these three functions be used?

Google, all of them are reprinted. The general content is as follows:

  1. Loadview manual loading View
  2. After viewdidload is used to load the NIB file
  3. Viewdidunload is the image of viewdidload.

For more information, see the official documentation:

I. loadview

Never take the initiative to call this function. View Controller calls this function when the view property is requested and the current view value is nil. If you manually create a view, you should reload this function. If you use IB to create a view and initialize the View Controller, it means that you use the initwithnibname: Bundle: method. In this case, you should not overload the loadview function.

The default implementation of this method is as follows: First find information about available nib files and load the nib files based on the information. If there is no information about nib files, by default, a blank uiview object is created, and the object becomes the main view of the controller.

So you should do the same when you reload this function. And assign the view of the subclass to the view property (the view you create must be a unique instance and not shared by any other controller ), in addition, the function you overload should not call super.

If you want to further initialize your views, you should do it in the viewdidload function. In IOS 3.0 and later versions, you should reload the viewdidunload function to release any reference to the view or its contents (Child view, etc ).

The materials on the Internet are not comprehensive, especially the blue part.

Ii. viewdidload

This function is called after the Controller loads related views, regardless of whether these views are stored in the NIB file or generated in the loadview function. In most cases, it is the subsequent work of the NIB file.

The description of this function is totally incorrect.

3. viewdidunload

This function is the opposite function of viewdidload. When the program memory is insufficient, this function is called by the Controller (). Since the Controller usually savesView(The view in bold here refers to the view attribute of the Controller) related objects (generallyViewSub-view) or references to other objects created at run time, so you must use this function to discard the ownership of these objects for memory reclaim. But do not release the hard-to-recreate data (do not release the data in this functionView).

Generally, the Controller will save the reference of views created by the NIB file, but it may also save the reference of objects created by the loadview function. The most perfect method is to use the synthesizer method:

self.myCertainView = nil;

In this way, the synthesizer will release the view. If you do not use the property, you have to explicitly release the view yourself.

The description of this function is vague on the Internet. I did not see it after reading it.

In addition, if the Controller stores references of other objects and views, you must release these memories in the dealloc method. For ios2.x, you must also set these references to nil before calling the super dealloc method.

Iv. Conclusion

So the process should be like this:

(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 the view, return to the first step.

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.