tags:

views:

81

answers:

3

The question is all in the title. I want to execute some cleanup code when one view in my application gets unloaded. Is it possible to do so? If so, which is the event that I should intercept?

+1  A: 

-viewDidUnload() or -viewWillDisappear() depending on your design. You probably want to go for viewDidUnload().

Björn
viewDidUnload() is only called if the view controller receives a memory warning, its view is not being displayed *and* it handles memory warnings in the default way (by just passing it to its parent class). This makes viewDidUnload() actually a bad place to do clean-up that may need to be done often. It will either need to be done on dealloc or a method may need to be added to do clean-up of critical resources.
Jason Coco
+1  A: 

It will also depend on the sdk you are using. If you are using iphone-sdk 3.x then viewDidUnload will be called otherwise it will not get called. while viewWillDisappear is called in 2.x and above. Now still if you want to call a method only when the view is unloaded you can call it from the dealloc but it will not be highly trustable.

Madhup
viewDidUnload is rarely called. For instance, if the view controller is simply deallocated, viewDidUnload will *not* be called. It is only called if the parent view unloads a view controller's view and the view controller itself is not deallocated.
Jason Coco
+1  A: 

You'll want to take a look at viewDidUnload or viewWillDisappear, for cleanup you'll probably use viewDidUnload, you may also want to just do cleanup in the dealloc method.

From Apple's documentation:

viewDidUnload Called when the controller’s view is released from memory.

- (void)viewDidUnload

Discussion This method is called as a counterpart to the viewDidLoad method. It is called during low-memory conditions when the view controller needs to release its view and any objects associated with that view to free up memory. Because view controllers often store references to views and other view-related objects, you should use this method to relinquish ownership in those objects so that the memory for them can be reclaimed. You should do this only for objects that you can easily recreate later, either in your viewDidLoad method or from other parts of your application. You should not use this method to release user data or any other information that cannot be easily recreated.

Typically, a view controller stores references to objects using an outlet, which is a variable or property that includes the IBOutlet keyword and is configured using Interface Builder. A view controller may also store pointers to objects that it creates programmatically, such as in the viewDidLoad method. The preferred way to relinquish ownership of any object (including those in outlets) is to use the corresponding accessor method to set the value of the object to nil. However, if you do not have an accessor method for a given object, you may have to release the object explicitly. For more information about memory management practices, see Memory Management Programming Guide for Cocoa.

By the time this method is called, the view property is nil.

Special Considerations If your view controller stores references to views and other custom objects, it is also responsible for relinquishing ownership of those objects safely in its dealloc method. If you implement this method but are building your application for iPhone OS 2.x, your dealloc method should release each object but should also set the reference to that object to nil before calling super.

viewWillDisappear: Notifies the view controller that its view is about to be dismissed, covered, or otherwise hidden from view.

- (void)viewWillDisappear:(BOOL)animated

Parameters animated If YES, the disappearance of the view is being animated.

Discussion This method is called in response to a view being removed from its window or covered by another view. This method is called before the view is actually removed or covered and before any animations are configured.

Subclasses can override this method and use it to commit editing changes, resign the first responder status of the view, or perform other relevant tasks. For example, you might use this method to revert changes to the orientation or style of the status bar that were made in the viewDidDisappear: method when the view was first presented. If you override this method, you must call super at some point in your implementation.

jessecurry