在 Xcode 中的视图中加载视图控制器 [英] Loading a View Controller inside a View In Xcode

查看:45
本文介绍了在 Xcode 中的视图中加载视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 iPad 上开发一个应用程序.该应用程序仅包含两个视图,但每个视图都包含许多按钮和标签.目前,两个视图的布局已经设置.每个都设置在一个视图控制器中.我还有另一个视图控制器,它包含顶部的主菜单和一个大容器(UIView),我希望它能够容纳我提到的两个视图.

I am current developing an app in iPad. The app consist of only two views, but each view contains a lot of buttons and labels. Current, the layout of the two views is set. Each one is set in a view controller. I also have another view controller which contains the main menu on top and a big container(UIView) in which I hope it will be able to hold the two views I mentioned.

我的问题是,有没有办法在视图中显示视图控制器?当我单击主菜单中的一个按钮时,我想在该容器(UIView)中显示一个视图控制器,并在单击另一个按钮时显示另一个视图控制器.如果我的计划是不可能的,那么请提出一些建议以使同样的事情奏效.

My question is, is there a way to show a view controller inside a view? I want to display one view controller inside that container(UIView) when I click on a button in the main menu, and display another when I click on another button. If my plan is not possible then please make some suggestions to make the same thing work.

非常感谢!

推荐答案

是的,您可以通过添加如下所示的 UIViewController 视图 轻松实现..

Yes you can easily do it by adding the UIViewController view like below..

_viewController=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
[self.view addSubview:viewController.view];

一旦你添加了 viewController.view,你的 viewDidLoad 方法在 ViewController 中就会被调用.

As soon as you add viewController.view your viewDidLoad method inside ViewController gets called.

更新:根据 UIViewController 类参考 在将 UIViewController 添加为另一个 ViewController 中的子视图时,您需要再添加两个步骤.

Update: As per UIViewController Class Reference you need to add two more steps when adding a UIViewController as subview inside another ViewController.

[self addChildViewController:viewcontroller];
[self.view addSubview:viewController.view];
[viewcontroller didMoveToParentViewController:self];

以上完成了答案.

希望这会有所帮助.干杯.

Hope this helps. Cheers.

这篇关于在 Xcode 中的视图中加载视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆