the View Management Cycle
The steps that occur during the load cycle are as follows:
-
Some part of your application asks for the view in the view controller’s
view
property. -
If the view is not currently in memory, the view controller calls its
loadView
method. -
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 theview
property. -
If you do not override this method, the default implementation uses the
nibName
andnibBundle
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 theview
property.
-
-
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:
-
The application 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 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.
-
-
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 |
Creating your view objects |
Overriding the |
|
Allocating or loading data to be displayed in your view |
Any data that is tied to your view objects should be created or loaded in the |
|
Releasing references to view objects |
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 |
|
Releasing data that is not needed when your view is not displayed |
You can use the |
|
Responding to low-memory notifications |
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 |
|
Releasing critical data structures required by your view controller |
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- |