2015-06-03 24 views

回答

0

首先,你需要得到你的故事板。爲了做到這一點,你應該使用UIStoryboard

UIStoryboard *firstStoryboard = [UIStoryboard storyboardWithName: @"FirstStoryboardName" bundle: nil]; 
UIStoryboard *secondStoryboard = [UIStoryboard storyboardWithName: @"SecondStoryboardName" bundle: nil]; 
UIStoryboard *thirdStoryboard = [UIStoryboard storyboardWithName: @"ThirdStoryboardName" bundle: nil]; 

接下來,你需要獲得初始視圖控制器來回那些故事板(假設firstStoryboard的視圖控制器包含這兩個容器)

UIViewController *secondVC = (UIViewController *)[secondStoryboard instantiateInitialViewController]; 
UIViewController *thirdVC = (UIViewController *)[thirdStoryboard instantiateInitialViewController]; 

現在添加這些包含容器的視圖控制器中的容器的兩個視圖控制器

UIViewController *firstVC = (UIViewController *)[firstStoryboard instantiateInitialViewController]; 
// TODO: Add secondVC and thirdVC as the children of firstVC 
0

您可以通過

[[UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"secondVCSrorybradID"]; 

指不同的故事板,你應該給你想要創建一個標識符的VC。

而且比你可以把它與addSubview添加到任何框架:

0

因此要回答關於加載項的問題g視圖控制器到容器... 容器視圖只是一個簡單的方法來添加子視圖控制器在界面生成器與segues。

因此,要以編程方式執行此操作,請首先從各個故事板實例化視圖控制器(請參閱Ch0k018的答案)。

屬於容器視圖的視圖控制器必須作爲子視圖控制器添加到主視圖控制器。你可以閱讀有關遏制這裏https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

// Add the child view as subview 
childViewController.view.frame = self.containerView.bounds; 
[self.containerView addSubview:childViewController.view]; 
// Need to call these methods to complete 
[self addChildViewController:tableViewController]; 
[childViewController didMoveToParentViewController:self]; 

從蘋果文檔:

下面的代碼做什麼:

它調用容器的addChildViewController:方法來添加子。調用addChildViewController:方法也會自動調用孩子的willMoveToParentViewController:方法。 它訪問子視圖屬性以檢索視圖並將其添加到其自己的視圖層次結構中。容器在添加視圖之前設置孩子的大小和位置;容器總是選擇孩子內容出現的地方。雖然此示例通過明確設置框架來實現此目的,但您也可以使用佈局約束來確定視圖的位置。 它明確地調用孩子的didMoveToParentViewController:方法來表示操作已完成。

相關問題