在另一个“嵌入”的UIViewController(“Embed” UIViewController

2019-07-29 15:04发布

我有一个UIViewController(DetailViewController)由在顶部和覆盖该屏幕的其余部分一个UIView一个导航栏。 是否有可能控制比其他DetailViewController一个UIViewController的UIView的?

Answer 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.



Answer 2:

你也可以做到这一切的故事板。 只要将容器从浏览到您的主视图控制器,并使用嵌入SEGUE从中嵌入式视图控制器。 它会正确设置所有视图控制器层次为您服务。



文章来源: “Embed” UIViewController within another