What is the -(void)viewDidUnload
is good for?
Could I not just relase everything in -dealloc
? If the view did unload, wouldn't -dealloc
be called anyway?
What is the -(void)viewDidUnload
is good for?
Could I not just relase everything in -dealloc
? If the view did unload, wouldn't -dealloc
be called anyway?
In addition to what has already been indicated, I wanted to elaborate more about logic behind
-viewDidUnload
.One of the most important reasons for implementing it is that
UIViewController
subclasses commonly also contain owning references to various subviews in the view hierarchy. These properties could have been set throughIBOutlets
when loading from a nib, or programmatically inside-loadView
, for instance.The additional ownership of subviews by the
UIViewController
means that even when its view is removed from the view hierarchy and released to save memory, through which the subviews are also released by the view, they will not actually be deallocated because theUIViewController
itself still contains its own outstanding retaining references to those objects as well. Releasing theUIViewController
additional ownership of these objects ensures they will be deallocated as well to free memory.The objects that you release here are usually recreated and set again when the
UIViewController
view isre-loaded
, either from a Nib or through an implementation of-loadView
.Also note that the
UIViewController
view
property isnil
by the time this method is called.As the documentation says:
In the same situation
dealloc
is not called. This method is only available in OS3 and above. Dealing with the same situation in iPhone OS 2.x was a real pain!Update July 2015: It should be noted that
viewDidUnload
was deprecated in iOS 6 because "Views are no longer purged under low-memory conditions and so this method is never called." So, the modern advice is not to worry about it and usedealloc
.This is because you will typically set the
@property
as"(nonatomic, retain)"
and as such the setter that is created for you releases the current object and then retains the argument i.e....does something along the lines of:
Therefore you are killing two birds with one stone: memory management (releasing the existing object) and assigning the pointer to nil (since sending any message to a nil pointer will return nil).
Hope that helps.
Remember that
viewDidUnload
is a method in the view controller, not in the view. The view'sdealloc
method will get called when the view unloads, but the view controller'sdealloc
method may not be called until later.If you get a low memory warning and your view isn't showing, which will happen for instance about any time you use a UIImagePickerController to let the user take a picture, your view will get unloaded and will need to get reloaded after that.
Conclusion:
View Controllers have a view property. Typically a nib or piece of code adds other views to this view. This happens often inside a -viewDidLoad method, like this:
in addition, a nib file may create a button and append it to the view controller's view.
On iPhone OS 2.2, when -didReceiveMemoryWarning was invoked from the system, you had to release something to free up memory. You could release the whole view controller's view if that made sense. Or just big memory-consuming contents in it.
Now, in the new OS 3.0, there is an -viewDidUnload method, which will be invoked from the system when the view has been unloaded because of low memory (please correct me: when exactly does this get called?)
-viewDidUnload is used to release all objects that were owned both by the view controller itself and the view. The reason: If a view controller holds references to childs of the view, i.e. a button, the referenced child views will not get released, because their retain count is >= 1. After they are released in -viewDidUnload, they can get freed up from memory.
Apple deprecated viewWillUnload, now you shoud use didReceiveMemoryWarning or dealloc to release your objetcs.
If the view controller is popped from the navigation controller stack and is not retained anywhere else, it will be deallocated, and dealloc will be called instead of viewDidUnload. You should release the views created in loadView in dealloc, but it is not necessary to set the variables to nil, because soon after dealloc is called the variables will no longer exist.
You can release any subviews you hold on to, for example that UIImageView you retained in your loadView method, or better yet the image that was on that UIImageView.