Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewDidAppear/ViewWillAppear not being called

I have a ViewController that adds to other subviews which have subclassed uiviewControllers

so Its like this:

mainViewController
       |
  v---------v
subVC1   subVC2

And neither subVC1 or subVC2 have the viewDidAppear/viewWillAppear fired on them? The main view controller is creating in the app delegate and it view added there as well to the window.

Here is an xcode project to show my problem: http://www.qfpost.com/download.do?get=92f03538907e72665ea794d98ff8392b

like image 362
Jonathan. Avatar asked Oct 17 '10 20:10

Jonathan.


People also ask

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

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).

Is viewWillAppear always called?

viewWillAppear(_:)Always called after viewDidLoad (for obvious reasons, if you think about it), and just before the view appears on the screen to the user, viewWillAppear is called.

What happens between viewWillAppear and viewDidAppear?

There is a noticeable pause after row selection and before the new view is pushed. Some logging indicates that all of my code is reasonably quick, from row selection until the pushed controller's viewWillAppear . But then the time between viewWillAppear and viewDidAppear is logged at around 0.7 seconds.

Can viewDidAppear be called multiple times?

viewDidAppear callback can be called multiple times in a single presentation.


2 Answers

I had a look at your code. So the problem is that viewWillAppear/viewDidAppear get called when the view is added to the Window, not to one of it's subview. You might want to use viewDidLoad for that.

like image 138
Julien Avatar answered Sep 19 '22 01:09

Julien


Firstly, are you sure you actually need to be doing what you're doing with this hierarchy of UIViewControllers? Why can't your MainViewController deal with the views that your subVC1 and subVC2 are responsible for?

Second, your viewWillAppear/viewDidAppear will get called when YOU write the code to call them from your MainViewController. Without knowing anything else about your design it's not really possible to advise on when you should call them, but if the sub-controllers' views are to be always visible when the 'main' view is visible, then it probably makes sense to call them from the viewWillAppear/viewDidAppear methods of your mainViewController.

like image 24
imaginaryboy Avatar answered Sep 18 '22 01:09

imaginaryboy