5

A question about didReceiveMemoryWarning / viewDidUnload.

If my app has many view controllers, one of them is shown, and the others back (because of I use a navigation controller or tab bar controller, it does not matter), which view controllers will receive didReceiveMemoryWarning / viewDidUnload, all of them, only hidden, or only shown?

Is it possible that shown VC receives didReceiveMemoryWarning but not viewDidUnload (because as is shown, it doesn't make any sense).

By the way, I have these questions after seeing this diagram: UIViewController init/dealloc flow chart

Thanks a lot for help.

Ricardo
  • 2,831
  • 4
  • 29
  • 42
  • From your diagram it follows that viewDidUnload is called only if didReceiveMemoryWarning is called ??? hmmm it is not true – Stas Feb 10 '12 at 13:18
  • That's the reason of my question. Do you know a better diagram? Thanks. – Ricardo Feb 10 '12 at 13:22
  • 1
    It seems this one has the answer: http://www.nextconceptdc.com/blog/wp-content/uploads/2011/08/UIViewControllerLifecycle.png – Ricardo Feb 10 '12 at 13:23
  • Read carefully the explanation text in the diagram @Richardo links. It explains it well. – Rob Napier Feb 10 '12 at 14:21

2 Answers2

1

First, there are two methods didReceiveMemwarnings:

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application

is called when the application receives a memory warning from the system. and UIViewController's

- (void)didReceiveMemoryWarning

Sent to the view controller when the application receives a memory warning.

Second, firstly is called the code in these methods(well, of course), then in those controllers which don't have superviews(i.e those which are not displayed at the moment) the view is deleted and viewDidUnload is sent

Stas
  • 9,925
  • 9
  • 42
  • 77
0

When a memory warning is received, it is received at application level, all your viewControllers and appdelegate receives it.

It is not necessary that viewDidUnload is called for any or all controllers. It is strictly dependent on how critical OS thinks that memory warning is. Like first time - your app receives a Level 1 warning then Level 2 and most likely at third time (in short interval) OS will terminate the application believing it has gone to unstable state.

Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41