why does viewDidAppear not get triggered?

13,657

Solution 1

You have to push the view controller on to a navigation stack in order for it's delegate methods to get called. Adding your view controller's view to the subview array won't call them. The first thing you should do is read the View Controller Programming Guide from Apple as this will save you from some headaches you're creating by doing this in a non-standard way.

Instead of adding the view to your root view controller subviews, do this:

SubviewController *controller = [[SubviewController alloc] init];
[[self navigationController] pushViewController:controller animated:YES];
[controller release], controller = nil;

Now your delegate methods will get called. If you don't have a navigation controller as your root view controller, though, this won't work.

Solution 2

If I recall correctly (sorry can't find the place in docs now) -viewDidAppear does not get called for subviews. You must call it manually in the -viewDidAppear method of parent view controller.

Share:
13,657
mbotta
Author by

mbotta

Updated on June 27, 2022

Comments

  • mbotta
    mbotta almost 2 years

    i have a root view controller that inserts a subview at index 0 at its viewDidLoad method.

    i am trying to get the subview to become firstResponder, but can only do this - from my understanding - in the subview's viewDidAppear method.

    here's the line of code i added to the root view controller's viewDidLoad method:

            [self.view insertSubview: subViewController.view atIndex: 0];
    

    the subviewcontroller has a xib, subViewController.xib, that is shown correctly at runtime. nevertheless, the subViewController's viewDidAppear does not get triggered.

    any idea why this happens? any idea how to remedy this - apart from calling viewDidAppear manually (doing so results in failure to become firstResponder)?

    thanks,

    mbotta