Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is viewWillDisappear called? and when is it not?

I have the following scenario:

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

  • instantiate vcB with NIB file - (when user taps a button in the UI)
  • initialize an iVar in vcB
  • present vcB with presentModalViewController
  • hits breakPoint in viewDidLoad of vcB
  • before the view from vcB is loaded, viewWillDisappear of vcA is called (I see it thru NSLog statements)
  • view from vcB is loaded
  • displays the correct value of its iVar (which was set in vcA)
  • dismiss vcB with dismissModalViewController

back in view of vcA -

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

  • instantiate vcB with NIB file
  • change the value of iVar in vcB
  • present vcB with presentModalViewController
  • this time, the breakpoint in viewDidLoad of vcB is NOT hit
  • before the view from vcB is loaded, viewWillDisappear of vcA is called this time too
  • view from vcB is loaded
  • displays the incorrect value of its iVar (it is the previous value)

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.

like image 771
Sam Avatar asked Mar 09 '11 06:03

Sam


People also ask

Is viewWillAppear always called?

It is called when ever the view appears on the screen. Yes even when the view controller is initialized or when you navigate to the view in a stack.

Why viewWillAppear is not called?

The Simple Answer. The technical reason for when viewWillAppear gets called is simple. Notifies the view controller that its view is about to be added to a view hierarchy. It can't be any view hierarchy — it has to be the one with a UIWindow at the root (not necessarily the visible window).

What is Viewwilldisappear?

Notifies the view controller that its view is about to be removed from a view hierarchy. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.0+ tvOS 9.0+

Why does viewWillAppear not get called when an app comes back from the background?

Swift. Use a NotificationCenter observer rather than viewWillAppear . To find out when an app comes back from the background, use a NotificationCenter observer rather than viewWillAppear .


1 Answers

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.

like image 104
Caleb Avatar answered Sep 19 '22 09:09

Caleb