在容器 ViewContorler 中显示多个 ViewController 的视图层次结构的正确方法是什么? [英] What is the correct way to display multiple ViewController's view hierarchy in a Container ViewContorler?

查看:16
本文介绍了在容器 ViewContorler 中显示多个 ViewController 的视图层次结构的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我在做一个有多个ViewControllers的项目,控制器的视图层次需要同时显示在屏幕上,下面的链接(是图片)是我的设计.

Recently, I am working on a project which have multiple ViewControllers, the controllers's view hierarchy need to display on screen at same time, the link below(it is a picture) is my design.

http://www.lazycatdesign.com/stuff/question.png

MainViewController 是一个容器 ViewController,我像这样添加 MenuViewController 和 PictureViewController:

MainViewController is a Container ViewController, I add the MenuViewController and PictureViewController to it like this:

// Create the controllers
MainViewContorller* mainVC = [[MainViewController alloc] init];
MenuViewController* menuVC = [[MenuViewController alloc] init];
PictureViewController* pictureVC = [[PictureViewController alloc] init];

// add MenuViewController to MainViewController as its child controller
[mainVC addChildViewController:menuVC];
[mainVC.view addSubview:menuVC.view];
[menuVC didMoveToParentViewController:mainVC];

// add PictureViewController to MainViewController as its child controller
[mainVC addChildViewController:pictureVC];
[mainVC.view addSubview:pictureVC.view];
[pictureVC didMoveToParentViewController:mainVC];

菜单视图和图片视图现在显示在屏幕上,问题是只有图片视图可以响应 UI 事件(例如点击手势).似乎只有我添加到 Container ViewController 的最后一个视图层次结构可以响应 UI 事件,为什么?在容器 ViewContorler 中显示多个 ViewController 的视图层次结构的正确方法是什么?

Menu View and Picture View is now displayed on screen, the problem is only the Picture View can response the UI Event(such as the Tap Gesture). It seems only the last view hierarchy I add to Container ViewController can response UI Event, why? and what is the correct way to display multiple ViewController's view hierarchy in a Container ViewContorler?

推荐答案

终于解决了,正如rdelmar所说,我忘记把frames设置到subviews了,apple的文档View Controller Programming Guide for iOS(第117页)也提到了这一点,代码应该是:

Finally, the problem is solved, as rdelmar said, I forget to set the frames to the subviews, apple's document View Controller Programming Guide for iOS (page 117) also mention this, the codes should be:

// Create the controllers
MainViewContorller* mainVC = [[MainViewController alloc] init];
MenuViewController* menuVC = [[MenuViewController alloc] init];
PictureViewController* pictureVC = [[PictureViewController alloc] init];

// add MenuViewController to MainViewController as its child controller
[mainVC addChildViewController:menuVC];
[menuVC setFrame:frameOfMenuView];          // set the correct frame to menu view
[mainVC.view addSubview:menuVC.view];       // add menu view as sub view to main view
[menuVC didMoveToParentViewController:mainVC];

// add PictureViewController to MainViewController as its child controller
[mainVC addChildViewController:pictureVC];
[pictureVC setFrame:frameOfPictureView];    // set the correct frame to picture view
[mainVC.view addSubview:pictureVC.view];    // add picture view as sub view to main view
[pictureVC didMoveToParentViewController:mainVC];

这篇关于在容器 ViewContorler 中显示多个 ViewController 的视图层次结构的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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