why does viewDidAppear not get triggered?

佐手、 提交于 2019-12-21 20:29:51

问题


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


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




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



来源:https://stackoverflow.com/questions/1718495/why-does-viewdidappear-not-get-triggered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!