Sam
Sam

Reputation: 829

When is viewWillDisappear called? and when is it not?

I have the following scenario:

In vcA (when user taps a button in the UI):

back in view of vcA -

repeat the process (to simulate user action of tapping the same button in UI):

So, I am majorly confused.

  1. Why is viewWillDisappear of vcA called? What are the conditions under which is is called?
  2. Why was viewDidLoad of vcB not called second time? Should I have used 'addSubview' instead?

Thanks in advance.... Sam.

Upvotes: 7

Views: 17386

Answers (1)

Caleb
Caleb

Reputation: 125017

As you might guess from the name, -viewWillDisappear is called whenever the view controller's view is about to be hidden, removed, etc. Full description on the UIViewController reference page.

-viewDidLoad and -viewWillDisappear are not a matched set. To conserve resources, and because some view controllers may never end up displaying their views, view controllers only load their views the first time they're actually needed. -viewDidLoad is called after that happens.

-viewWillAppear and -viewDidAppear are called just before and after the view is actually displayed. Likewise, -viewWillDisappear and -viewDidDisappear are called before and after the view is no longer visible.

Finally, -viewDidUnload is the counterpart to -viewDidLoad and is called if the view is discarded. That can happen when the system needs to free up some memory, but it may not happen at all.

In iOS 6 viewWillUnload and viewDidUnload are Deprecated

So, to address your second question directly, vcB's -viewDidLoad wasn't called a second time because by that time vcB had already loaded its view and didn't need to do it again.

Upvotes: 12

Related Questions