IOS development (30)-UIViewController Memory Management

Source: Internet
Author: User


UIViewController memory management for iOS development is described in this article. After iOS 3.0, UIViewController adds a method called viewDidUnLoad. Many people do not know the specific significance of this method. Apple's document simply explains "Called when the controller's view is released from memory, and ask you to clear the view bound to the IBOutlet. Why?

Let's take a look at several functions of the process from creating a view to displaying a UIViewController.

-InitWithNibName: bundle:

This method initializes a vc, but note that the view is not loaded at this time.


-LoadView

-ViewDidLoad

When a view is ready to be displayed, vc first checks whether the view has been created. Otherwise, it initializes the view through the previously specified xib file and binds other relationships (if the xib file is not specified, by default, xib with the same name as vc will be searched. For example, myNameViewController will search for myNameViewController. xib file)

If there is no xib file, you can manually create the view required by viewControoler in loadview. The next step is to call-viewDidLoad. Many people like to do other things here, such as making an http request and creating an array. If this is not correct, the memory will be easily leaked when viewDidUnload is activated, which will be mentioned later.

-View () appear

-View () disappear

These methods are not explained.

-ViewDidUnload

This method is called when the memory warning is received and the view is not displayed on the current interface. The view of the controller has been released and assigned as nil.

Next, you need to release the sub-view of the instance variables (IBOulet and added by yourself) and other instance variables, such as the data array and http request of the instance in-viewDidLoaded.

Because when the viewController is activated again to prepare for display (for example, navigationControler returns to the upper level), vc finds that its view is empty and repeats the previous process until the view is created. If you do not release the added sub-views and various instance variables, the sub-views will be created again. Therefore, the memory is leaked.

In mac OS, if the control is connected using IBOutlet without the @ property (retain) attribute, release is not required in dealloc. If the retai attribute exists, release is required.

 


 

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.