2013-06-04 31 views
0

下面是我的代碼:如何將具有特定ID的故事板加載到UIViewController?

UIViewController *viewController = [UIViewController new]; 
if (indexPath.section == 0){ 
     UIViewController *controller; 
     switch (indexPath.row) { 
      case 0: 
       controller = [[self storyboard] instantiateViewControllerWithIdentifier:@"ProfileSettings"]; 
       [[viewController view] setBackgroundColor:[UIColor greenColor]]; 

所有我要的是「負荷」控制器到的viewController,但我不知道這是否是可能的..可以任你解釋我做錯了什麼?

回答

0

我不知道爲什麼你需要先創建一個新的視圖控制器,然後實例化另一個視圖控制器,並將其放置在您最初創建的視圖控制器中。

這將是更好的做這樣的事情:

id viewController; 
if (indexPath.section == 0) { 
    switch (indexPath.row) { 
     case 0: 
      viewController = [[self storyboard] instantiateViewControllerWithIdentifer:@"ProfileSettings"]; 
    // etc etc 

// Display view controller as required 

...但是,如果你實際上要做的是把你的故事板實例化視圖控制器內的另一個視圖控制器的然後你必須使用視圖控制器遏制,這是從iOS 5開始提供的。視圖控制器遏制工程是這樣的:

[vc addChildViewController:instantiatedController]; 
[vc.view addSubview:instantiatedController.view]; 

但我不確定是否真的需要這樣做。從您發佈的代碼看起來像沒有真正需要使用遏制。

相關問題