在Container ViewController中显示多个View Controllers视图层次结构的正确方法是什么?

时间:2014-05-16 00:56:22

标签: ios objective-c uiviewcontroller

最近,我正在开发一个有多个ViewControllers的项目,控制器的视图层次结构需要同时显示在屏幕上,下面的链接(这是一张图片)是我的设计。

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

MainViewController是一个Container ViewController,我将MenuViewController和PictureViewController添加到它中:

// 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];

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

1 个答案:

答案 0 :(得分:0)

最后,问题解决了,正如rdelmar所说,我忘了将帧设置为子视图,apple的文档View Controller Programming Guide for iOS(第117页)也提到了这一点,代码应该是:

// 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];