8
votes

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

5
So this works on sdk3 but not on earlier ones?EightyEight
See Ramin's answer about the viewWillAppear mystery. It's in the docs, but its not easy to find. Not sure why viewControllerB never makes it to the screen. Are you doing any important initialization in viewWillApeear?amattn
+1 for list of things checked before asking question herestefanB
No, I perform no initialization outside viewDidLoad(s) And yes it works in 3.0, I'm continuing to do my normal coding by compiling for 3.0, but I really want to have this compile in 2.2.1 as wellCorey Floyd

5 Answers

22
votes

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.

3
votes

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.

1
votes

Try

-(void) viewDidAppear:(BOOL)animated {
// whatever
}
1
votes

I guess your view is not the root view. You are probably using a UINavigationController so try to implement this method

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated

or this one

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

and add in your viewDidLoad

this.navController.delegate = self;

(don't forget to add the UINavigationControllerDelegate protocol in your .h file.

0
votes

To close the matter:

I could never find a solution, but since then SDK 3.0 is the standard and the point has become moot. The code does work correctly in current versions of the SDK.