2014-05-16 40 views
0

最近,我正在研究一個有多個ViewController的項目,控制器的視圖層次結構需要同時在屏幕上顯示,下面的鏈接(這是一張圖片)就是我的設計。在Container ViewContorler中顯示多個ViewController的視圖層次結構的正確方法是什麼?

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

MainViewController是一個容器視圖控制器,我添加了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]; 

菜單查看和圖看,現在顯示在屏幕上,問題是隻有圖片View可以響應UI事件(例如Tap Gesture)。看來只有我添加到Container ViewController的最後一個視圖層次可以響應UI事件,爲什麼?以及在Container ViewContorler中顯示多個ViewController的視圖層次結構的正確方法是什麼?

+0

您需要將框架(或添加約束)設置爲您要添加的子視圖。該視圖的不響應可能是由於視圖超出其超視圖範圍或零大小。 – rdelmar

+0

你想要改變viewController中的按鈕操作的權利? –

回答

0

最後,問題被解決了,rdelmar說,我忘了幀設置爲子視圖,蘋果的文件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]; 
相關問題