2013-04-11 56 views
0

這是按下按鈕的代碼:編程初始化導航控制器堆棧

- (IBAction)newPoint:(id)sender { 
    [self.tabBarController setSelectedIndex:1]; 

    UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"navid"]; 
    UIViewController *tblvc = [self.storyboard instantiateViewControllerWithIdentifier:@"tblid"]; 

    UINavigationController *nvc1 = self.tabBarController.viewControllers[1]; 
    [nvc1 pushViewController:tblvc animated:NO]; 
    [nvc1 pushViewController:vc animated:NO]; 
} 

這是我的故事板 enter image description here

這是例外,我得到了:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported' 

我」什麼m試圖實現的是在按下按鈕時顯示最右邊的視圖控制器(在故事板中)。然後,我想讓導航控制器的後退按鈕進入根視圖控制器,就像我手動到達該控制器一樣。爲此,我更改爲第二個選項卡(因爲按鈕位於第一個選項卡上),然後我嘗試初始化導航控制器。但有一些我可能會丟失...

回答

1

確保您的故事板ID設置在故事板中正確的視圖控制器上。對我來說,它看起來像任何

UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"navid"]; 

或者

UIViewController *tblvc = [self.storyboard instantiateViewControllerWithIdentifier:@"tblid"]; 

是創建一個UINavigation控制器的新實例。您可以使用以下確認:

- (IBAction)newPoint:(id)sender { 
     [self.tabBarController setSelectedIndex:1]; 

     UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"navid"]; 
     UIViewController *tblvc = [self.storyboard instantiateViewControllerWithIdentifier:@"tblid"]; 

     NSLog(@"vc - %@",NSStringFromClass([vc class])); 
     NSLog(@"tblvc - %@",NSStringFromClass([tblvc class])); 

     UINavigationController *nvc1 = self.tabBarController.viewControllers[1]; 
     [nvc1 pushViewController:tblvc animated:NO]; 
     [nvc1 pushViewController:vc animated:NO]; 
    } 

然後檢查調試器中的類名稱。無論哪個是UINavigationController(或其子類)都是問題所在。

+0

你是對的,身份證是不正確的。但是,整體而言,還是有更好的方法呢?謝謝 – Pablo

+0

是的,我會說你的方法是完全正確的。 –

+0

太好了,謝謝你的時間! – Pablo

相關問題