Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is UIViewController viewDidUnload called?

Note: This question is outdated—viewDidUnload is deprecated iOS 6.

When does UIViewController's viewDidUnload automatically get called? Yes I know, when the view unloads. But when does that happen automatically? How can I do it manually? Thanks.

like image 978
mk12 Avatar asked Aug 17 '09 02:08

mk12


People also ask

What is UIViewController life cycle?

The LifecycleThe view controller lifecycle can be divided into two big phases: the view loading and the view lifecycle. The view controller creates its view the first time the view is accessed, loading it with all the data it requires. This process is the view loading.

Which of the following method is called after UIViewController is initialized?

First UIViewController is alloc'ed by some other object, then init is immediately called (or some other init method, like initWithStyle). Only once the object is initialized would I expect it to call its own loadView function, after which the view, once loaded, calls the viewDidLoad delegate method.

Is UIViewController a subclass of Uiview?

Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy. A view controller's main responsibilities include the following: Updating the contents of the views, usually in response to changes to the underlying data.

What is UIViewController Swift?

A UIViewController is an object which manages the view hierarchy of the UIKit application. The UIViewController defines the shared behavior and properties for all types of ViewController that are used in the iOS application. The UIViewController class inherits the UIResponder class. ADVERTISEMENT. ADVERTISEMENT.


2 Answers

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

That's because view controllers by default are registered for memory warning notifications, and any view that is not currently being used will be unloaded by the view controller - the viewDidUnload method is there so that you can clean up anything else you would like, to save extra memory (or if you've retained some IBOutlets to help free up memory that would otherwise be released by the view being unloaded).

Generally any IBOutlets you release in dealloc, should also be released (and references set to nil) in this method.

like image 104
Kendall Helmstetter Gelner Avatar answered Oct 01 '22 08:10

Kendall Helmstetter Gelner


In addition to manually issuing a memory warning in the simulator, you can issue one programatically with

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

You can then cause this to happen 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]; } 
like image 34
johnboiles Avatar answered Oct 01 '22 07:10

johnboiles