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:
-
The load cycle is triggered when the view controller’s
view
property is accessed and the view is not currently in memory. -
The view controller calls its
loadView
method. The default implementation of theloadView
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 theview
property.
-
-
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.
Loading a view into memory
The steps that occur during the unload cycle are as follows:
-
The app receives a low-memory warning from the system.
-
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 callsuper
at some point in your implementation to ensure that the default implementation runs. The default implementation attempts to release the view. -
If the view cannot be safely released (for example, it is visible onscreen), the default implementation returns.
-
The view controller calls its
viewWillUnload
method to inform subclasses that the views are about to be removed. A subclass typically overrides theviewWillUnload
method when it needs to save any view properties before the views are destroyed. -
It sets its
view
property tonil
. -
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.
Unloading a view from memory