views:

1347

answers:

2

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  A: 

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.

Vladimir
really? ouch, that is unfortunate: i should have found that in the docs myself and becomeFirstResponder somehow doesn't like when i call viewDidAppear explicitly. thanks though, i will try and find the reference.
mbotta
+5  A: 

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.

Matt Long
thanks! that is, in fact, the case: i don't intend to use navigation controllers in this project. i read through the programming guide you mentioned (thanks for that) yesterday, just to be sure, but i really can't find anything to help me out.getting... desperate.
mbotta
I guess my point is that what you are trying to do is not advisable precisely because you can't find a solution. You may have a great idea for a way to present views that is different from the standard and that may be great, however, in general you'll have a lot easier time sticking to what Apple has given you with either a navigation stack or tabbed views. Maybe if you elaborate on what you are trying to do, people on SO can make some suggestions for a different yet adequate approach that suits your requirements. Best regards.
Matt Long
good point. in fact, as i will point out in an answer to my own question later, i've chosen to abandon this particular design for a simpler one: the utility template, i believe it's called (not at my mac right now).with that template, there is no root view controller to bother with. there is one main view that is the delegate of a flip view. both get triggered appropriately, which was the point of my design in the first place.thanks for putting that bug in my head!
mbotta