3
votes

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

2

2 Answers

15
votes

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.

10
votes

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.