The intent here is to "balance out" your subview management. Anything that you create in viewDidLoad
should be released in viewDidUnload
. This makes it easier to keep track of what should be released where. In most cases, your dealloc
method is a mirror-image of your init
method, and your viewDidUnload
will be a mirror image of your viewDidLoad
method.
As you pointed out, the viewDid
... methods are to be used when the view itself is loaded and unloaded. This permits a usage pattern in which the view controller remains loaded in memory, but the view itself can be loaded and unloaded as required:
init
viewDidLoad
viewDidUnload
viewDidLoad
viewDidUnload
...
dealloc
Of course, it doesn't hurt to release things in your dealloc
method as well, as long as you set them to nil
when you release them in viewDidUnload
.
The following quote from the Memory Management section of Apple's UIViewController
documentation, describes it in more detail:
...in iPhone OS 3.0 and later, the viewDidUnload method may be a more appropriate place for most needs.
When a low-memory warning occurs, the UIViewController class purges its views if it knows it can reload or recreate them again later. If this happens, it also calls the viewDidUnload method to give your code a chance to relinquish ownership of any objects that are associated with your view hierarchy, including objects loaded with the nib file, objects created in your viewDidLoad method, and objects created lazily at runtime and added to the view hierarchy. Typically, if your view controller contains outlets (properties or raw variables that contain the IBOutlet keyword), you should use the viewDidUnload method to relinquish ownership of those outlets or any other view-related data that you no longer need.