0

I'm using viewWillAppear, viewDidAppear, viewWillDisappear, viewDidDisappear of UIViewController.
And I use UINavigationController to navigate view controllers.

For example, I create NSTimer or register notifications in viewWillAppear or viewDidAppear. And I invalidate the timer or remove notifications in viewWillDisappear or viewDidDisappear.

But if those 4 methods (viewWillAppear, viewDidAppear, viewWillDisappear, viewDidDisappear) are not called, the program will crash or retain cycle will happen.

I couldn't find the documentation saying that it is guaranteed that viewWillAppear, viewDidAppear, viewWillDisappear, viewDidDisappear are always called.

Till now, they seem to be always called and my program works as I expected.

But are there cases that the 4 methods are not called?
Or is it possible the 4 methods are usually called but sometimes not called randomly with no reason?

js_
  • 4,671
  • 6
  • 44
  • 61

6 Answers6

3

I can say than viewWillAppear and viewDidAppear methods are always called in your view. viewWillDisappear and viewDidDisappear methods are called when you change your view with another one or close it. If you have some scrolling views for example, these methods won't be called.

Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
2

Yes, they are always called. Just make sure to call the superclass implementation if you subclass one of your view controllers.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
  • I'm noticing that viewWillDisappear: and viewDidDisappear: are not being called when a phone call is received. – Murray Sagal Jan 28 '14 at 14:09
  • Hmm, that's interesting @MurraySagal. I've never tested this scenario before. I'm no longer doing iOS development, so I don't have access to a device to test this. – Michael Mior Jan 29 '14 at 14:49
  • Correct, viewWillDisappear is NOT called if your app is backgrounded. This is because the view stack did not change in this case. – lostintranslation Jul 16 '15 at 21:41
1

I think you can trust those methods, still, viewDidLoad and dealloc for me never failed.

You can register and remove notifications there. But, I don't know all of your app specifications and what I recommend might not be viable.

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
1

Yes the View events always fire, however to override the behaviour you must declare them.

According to apple documentation viewDidUnload is deprecated.

Look at this link for the official apple class reference for UIViewController

UIViewController Reference

It states:

The UIViewController class provides specific methods that are called when these events occur. Subclasses can override these methods to implement specific behaviors.

IsakBosman
  • 1,453
  • 12
  • 19
0

Here are the same thread in which same thing asked m any times you can view these here

here the first link

here the second one

Community
  • 1
  • 1
Kamar Shad
  • 6,089
  • 1
  • 29
  • 56
0

Aside from bugs when some of the methods may not get called and/or called twice (e.g. dismissing/popping several controllers at once), with iOS 7, if you initiate the swipe-to-back gesture, partially revealing the previous controller in the navigation stack, and then cancel the swipe-to-back, so that no popping occurs, you will observe that not all of the methods get called on both of the controllers.

Sea Coast of Tibet
  • 5,055
  • 3
  • 26
  • 37