When is viewWillDisappear called? and when is it not?

I have the following script:

In vcA (when the user enters a button in the user interface):

  • create a vcB instance with the NIB file - (when the user enters a button in the user interface)
  • initialize iVar to vcB
  • present vcB with presentModalViewController
  • removes breakPoint in viewDidLoad vcB
  • before the view from vcB is loaded, viewWillDisappear vcA is called (I see this through NSLog instructions)
  • view from vcB loaded
  • displays the correct value of its iVar (which was set to vcA)
  • reject vcB using offsetModalViewController function

back with vcA -

repeat the process (to simulate user action when clicking the same button in the user interface):

  • create vcB instance with NIB file
  • change iVar to vcB
  • present vcB with presentModalViewController
  • this time breakpoint in viewDidLoad vcB did NOT hit
  • before the view from vcB is loaded, viewWillDisappear from vcA is also called this time
  • view from vcB loaded
  • displays the wrong value of its iVar (this is the previous value)

So, I'm very confused.

  • Why is viewVillDisappear from vcA called? What are the conditions under which called?
  • Why was viewDidLoad vcB not called a second time? Should I use 'addSubview' instead?

Thanks in advance ... Sam.

+4
source share
1 answer

As you can guess from the name, -viewWillDisappear is called whenever the view of the view controller is hidden, deleted, etc. Full description on the UIViewController Link Page .

-viewDidLoad and -viewWillDisappear do not match the set. To save resources, and since some view controllers can never display their views, view managers only load their views the first time they are really needed. -viewDidLoad is called after this.

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

Finally, -viewDidUnload is a partner of -viewDidLoad and is called if the view is dropped. This can happen when the system needs to free some memory, but it may not happen at all.

In iOS 6, viewWillUnload and viewDidUnload are viewDidUnload

So, to directly address your second question, vcB -viewDidLoad was not called a second time because by then vcB had already loaded its view and no longer needed it.

+11
source

Source: https://habr.com/ru/post/1342946/


All Articles