我有一个 UIViewController
,里面有一个 UIContainerView
。基于某个条件是否为真,我想以编程方式将容器 View 的嵌入式 View 设置为不同的 UIViewController
。我注意到你只能有一个嵌入的 segue 来设置一个 UIViewController
,那么有没有办法做到这一点?
我尝试将我的容器 View 设置为导出,但我找不到任何设置嵌入式 UIViewController
的方法。任何关于如何开始使用的建议都将不胜感激。
最佳答案
如果你的意思是交换 UIViewController
的 UIViews
,我认为你的想法是错误的(希望我理解你的概念)。
UIViewController
应该有 1 个设计好的 UIView
并且应该管理那个 View 的值。正如您所说,您可以使用 Containers
,但是您应该添加 UIViewController
及其 View ,因此有一个管理此 View 的实例。您的第一个 UIViewController
应该只添加/删除那个 ChildViewController
。
所以我建议:
在 UIViewController 上实现一个类别并添加以下方法:
- (void)displayContentController:(UIViewController *)content {
[self addChildViewController:content];
content.view.frame = [[UIScreen mainScreen] bounds];
[self.view addSubview:content.view];
[content didMoveToParentViewController:self];
}
- (void)hideContentController:(UIViewController *)content {
[content willMoveToParentViewController:nil];
[content.view removeFromSuperview];
[content removeFromParentViewController];
}
创建AViewController
和BViewController
。在 AViewController
中调用(viewDidLoad?):
BViewController *bViewController = [[BViewController alloc] init];
[self displayContentController:bViewController];
在 BViewController
中管理这个 Controller 的 View 。 AViewController
应该只管理何时显示 BViewController 以及何时隐藏它。
如果我误解了你的问题,请发表评论,我会删除这个答案。
关于iOS - 以编程方式设置 UIContainerView 的嵌入式 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30475235/