(Loading a view into memory)and( Unloading a view from memory)

Understanding How Views Are Loaded and Unloaded

In a view controller object, management of the corresponding view occurs in two distinct cycles: the load and unload cycles. The load cycle occurs whenever some part of your app asks the view controller for its view object and that object is not currently in memory. When that happens, the view controller loads the view into memory and stores it in its view property for future reference.

If your app receives a low-memory warning at some point in the future, the view controller may subsequently try to unload the view. During the unload cycle, the view controller attempts to release its view object and return the view controller to its initial, viewless state. If it is able to release the view, the view controller remains without a view object until the view is once again requested, at which point the load cycle begins again.

During the load and unload cycles, the underlying implementation of the UIViewController class does most of the work of loading and unloading the view. However, if your view controller class stores references to views in the view hierarchy or needs to perform additional configuration of the views at load time, you can override specific methods (which are described in the material that follows) to perform any extra tasks.

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

  1. The load cycle is triggered when the view controller’s view property is accessed and the view is not currently in memory.

  2. The view controller calls its loadView method. The default implementation of the loadView method does one of two things:

    • If the view controller is associated with a storyboard, it loads the views from the storyboard.

    • If the view controller is not associated with a storyboard, an empty UIView object is created and assigned to the view property.

  3. The view controller calls its viewDidLoad method to allow your subclass to perform any additional load-time tasks.

Figure 4-1 shows a visual representation of the load cycle, including several of the methods that are called. Your app can override both theloadView and the viewDidLoad methods as needed to facilitate the behavior you want for your view controller. For example, if your app does not use storyboards but you want additional views to be added to the view hierarchy, you override the loadView method to instantiate these views programatically.

Figure 4-1  Loading a view into memory



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

  1. The app 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 memory or objects that your view controller object no longer needs. Do not use it to release your view controller’s view. You must call super at some point in your implementation to ensure that the default implementation runs. The default implementation attempts to release the view.

  3. If the view cannot be safely released (for example, it is visible onscreen), the default implementation returns.

  4. The view controller calls its viewWillUnload method to inform subclasses that the views are about to be removed. A subclass typically overrides the viewWillUnload method when it needs to save any view properties before the views are destroyed.

  5. It sets its view property to nil.

  6. The view controller calls its viewDidUnload method to inform subclasses that the views were removed. A subclass typically uses this method to release any strong references it has to those views.

Figure 4-2 shows a visual representation of the unload cycle for a view controller.

Figure 4-2  Unloading a view from memory

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值