Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewDidLoad gets called, viewWillAppear does not get called, view does not appear on screen

Update

It has been mentioned that viewWillAppear and viewDidAppear do not get called per the docs. However, I perform no initialization within these methods and no adding UI elements to the view.

I was just using them to place break points to try to debug this.

Any other ideas?


Original Question

I'm stumped. I'm refactoring some code and have come across some strange behavior....

I initialize viewController A without a nib and set the view programmatically.

viewDidLoad in controller A gets called.

Inside viewDidLoad of viewController A, I initialize viewController B from a nib.

I add viewControllerB.view as a subview of viewControllerA.view

viewDidload of controller B gets called.


Then it gets weird:

  1. viewWillAppear and viewDidAppear of viewController B never get called.

  2. viewControllerB.view never makes it on screen.

  3. No errors.


Things I checked:

The name of the viewControllerBs Nib is correct.

The view outlet of viewControllerB is connected to a view.

viewControllerB and its view both are non-nil.

And to top it off, everything works great in SDK [redacted] beta 5!

Any ideas? It's got to be something silly..

like image 696
Corey Floyd Avatar asked May 21 '09 23:05

Corey Floyd


People also ask

Does viewWillAppear get called before viewDidLoad?

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.

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 the difference between viewDidLoad and viewWillAppear?

The difference between viewDidAppear and viewDidLoad is that viewDidAppear is called every time you land on the screen while viewDidLoad is only called once which is when the app loads. Let me show you a simple experiment to help you to understand how it works.

What triggers viewWillAppear?

The method viewWillAppear: is triggered in response to a change in the state of the application, indicating that the view controller is becoming “active.” The reason viewDidLoad exists – the only reason – is that it sometimes isn't possible or efficient to configure 100% of an interface in a XIB.


2 Answers

If the view controller is added to a view hierarchy through code, the view controller will not get viewWillAppear (or viewDidAppear) messages. If you add it yourself you have to send the view controller the message yourself.

like image 65
Ramin Avatar answered Sep 18 '22 15:09

Ramin


Don't forget to call

[super viewWillAppear:animated];

Wherever you redefine it.

The documentation says:

If you override this method, you must call super at some point in your implementation.

like image 42
Antoine Avatar answered Sep 18 '22 15:09

Antoine