zoukankan      html  css  js  c++  java
  • view的内存管理

    the View Management Cycle

    The steps that occur during the load cycle are as follows:

    1. Some part of your application asks for the view in the view controller’s view property.

    2. If the view is not currently in memory, the view controller calls its loadView method.

    3. The loadView method does one of the following:

      • If you override this method, your implementation is responsible for creating all necessary views and assigning a non-nil value to the view property.

      • If you do not override this method, the default implementation uses the nibName and nibBundle properties of the view controller to try to load the view from the specified nib file. If the specified nib file is not found, it looks for a nib file whose name matches the name of the view controller class and loads that file.

      • If no nib file is available, the method creates an empty UIView object and assigns it to the view property.

    4. The view controller calls its viewDidLoad method to perform any additional load-time tasks.

    The steps that occur during the unload cycle are as follows:

    1. The application receives a low-memory warning from the system.

    2. Each view controller calls its didReceiveMemoryWarning method:

      • If you override this method, you should use it to release any custom data that your view controller object no longer needs. You should not use it to release your view controller’s view. You must call super at some point in your implementation to perform the default behavior.

      • The default implementation releases the view only if it determines that it is safe to do so.

    3. If the view controller releases its view, it calls its viewDidUnload method. You can override this method to perform any additional cleanup required for your views and view hierarchy.

    • How do you allocate memory efficiently?

    • When and how do you release memory?

    Task

    Methods

    Discussion

    Allocating critical data structures required by your view controller

    Initialization methods

    Your custom initialization method (whether it is named init or something else) is always responsible for putting your view controller object in a known good state. This includes allocating whatever data structures are needed to ensure proper operation.

    Creating your view objects

    loadView

    Overriding the loadView method is required only if you intend to create your views programmatically. If you are loading your views from a nib file, all you have to do is use the initWithNibName:bundle: method to initialize your view controller with the appropriate nib file information.

    Allocating or loading data to be displayed in your view

    viewDidLoad

    Any data that is tied to your view objects should be created or loaded in the viewDidLoad method. By the time this method is called, your view objects are guaranteed to exist and be in a known good state.

    Releasing references to view objects

    viewDidUnload

    dealloc

    If you retain any view objects in your view hierarchy using outlets or other member variables in your class, you must always release those outlets when the views are no longer needed. After releasing a view object, always be sure to set your outlet or variable to nil for safety. For more information about when views get released, see “Understanding the View Management Cycle.”

    Releasing data that is not needed when your view is not displayed

    viewDidUnload

    You can use the viewDidUnload method to deallocate any data that is view-specific and that can be recreated easily enough if the view is loaded into memory again. If recreating the data might be too time-consuming, though, you do not have to release the corresponding data objects here. Instead, you should consider releasing those objects in your didReceiveMemoryWarning method.

    Responding to low-memory notifications

    didReceiveMemoryWarning

    Use this method to deallocate all noncritical custom data structures associated with your view controller. Although you would not use this method to release references to view objects, you might use it to release any view-related data structures that you did not already release in your viewDidUnload method. (The view objects themselves should always be released in the viewDidUnload method.)

    Releasing critical data structures required by your view controller

    dealloc

    Use this method to release all data structures associated with your view controller. If your view controller still has outlets or other variables with non-nil values, you should release them here.

  • 相关阅读:
    Hdu 1257最少拦截系统
    删除mysql__转
    sql 入门的小例子熟悉一下_这可是一个转转转贴 :)
    header 用法_转
    java_json 转换 文件在file中
    javascript_php 正则匹配 转
    mysql 忘记密码转_kinghu
    php 通用下载
    明天就是新年开始
    翻译 有助于程序命名
  • 原文地址:https://www.cnblogs.com/woainilsr/p/2368869.html
Copyright © 2011-2022 走看看