“Embed” UIViewController within another

五迷三道 提交于 2019-12-21 20:18:10

问题


I have a UIViewController (DetailViewController) consisting of a navigation bar on the top and a UIView covering the rest of the screen. Is it possible to control the UIView with a UIViewController other than DetailViewController?


回答1:


You can do this, but you must not forget to call Apple's required methods for embedding UIViewControllers. Otherwise, your view controller will not get called by the OS to handle certain events.

To add the view controller:

[self addChildViewController:childViewController];                 
[self.view addSubview:childViewController.view];
[childViewController didMoveToParentViewController:self];

To remove the view controller:

[childViewController willMoveToParentViewController:nil];  
[childViewController.view removeFromSuperview];            
[childViewController removeFromParentViewController];

Related documentation:

  • Implementing a Container View Controller in the UIViewController Class Reference
  • Implementing a Container View Controller in the View Controller Programming Guide for iOS

See this question for more information.




回答2:


You can also do all this in storyboard. Just drag a container view out into your main view controller and use the embed segue from it to your embedded view controller. It will properly set up all the view controller hierarchy for you.



来源:https://stackoverflow.com/questions/10808667/embed-uiviewcontroller-within-another

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