When is the UIViewController viewDidUnload called?

Note. This question is deprecated - viewDidUnload deprecated iOS 6.

When is the UIViewController viewDidUnload automatically called? Yes, I know when a view is unloaded. But when does this happen automatically? How can I do this manually? Thank.

+45
iphone uiviewcontroller
Aug 17 '09 at 2:27
source share
5 answers

If you issue a memory warning in the simulator (see the menu), this will cause a call for any view controller attached to a view that is not visible.

This is because, by default, view controllers are registered for memory warning notifications, and any view that is not currently in use will be unloaded by the view controller - there is a viewDidUnload method so that you can clear anything you want to save additional memory (or if you saved some IBOutlets to free memory that would otherwise be released when the view is unloaded).

Generally, any IBOutlets that you release in dealloc should also be freed (and the links are set to zero) in this method.

+42
Aug 17 '09 at 2:29
source share

In addition to manually issuing a memory warning in the simulator, you can program once with

 - (void)_simulateLowMemoryWarning { // Send out MemoryWarningNotification [[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidReceiveMemoryWarningNotification object:[UIApplication sharedApplication]]; // Manually call applicationDidReceiveMemoryWarning [[[UIApplication sharedApplication] delegate] applicationDidReceiveMemoryWarning:[UIApplication sharedApplication]]; } 

Then you can call it every 5 seconds using a timer

 static NSTimer *gLowMemoryTimer = nil; - (void)stopLowMemoryTimer { [gLowMemoryTimer invalidate]; gLowMemoryTimer = nil; } - (void)startLowMemoryTimer { if (gLowMemoryTimer) { [self _stopLowMemoryTimer]; } gLowMemoryTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(_simulateLowMemoryWarning) userInfo:nil repeats:YES]; } 
+37
Jun 16 '10 at
source share

-viewDidUnload is called whenever the viewcontroller's view property is set to nil, either manually, or most often through didReceiveMemoryWarning:

+17
Aug 17 '09 at 3:14
source share

iOS 6.x and later

I know this is an older question, but I feel that an answer should be provided regarding changes to the viewDidUnload API in iOS 6, namely that in iOS 6, viewDidUnload no longer called (in general) and deprecated.

+9
Oct 24 '12 at 15:53
source share

viewDidUnload is called in low memory conditions. We must unload the material that we loaded in the viewDidLoad method. We need to give up ownership of the object by calling the access method to set it to zero. In the event of an exit, the object is freed, so the reference to the object can be safely set to zero. If not a synthesized property, then we first need to free the object than we set to nil.

+3
Jan 24 '12 at 10:01
source share



All Articles