开发者

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, viewWill开发者_运维知识库Disappear 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜