以编程方式初始化导航控制器堆栈

时间:2013-04-11 20:05:25

标签: ios ios5

这是按下按钮的代码:

- (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'

我想要实现的是在点击按钮时显示最右边的视图控制器(在故事板中)。然后我希望导航控制器的后退按钮进入根视图控制器,就像我手动到达该控制器一样。

为此,我更改为第二个选项卡(因为按钮位于第一个选项卡上),然后我尝试初始化导航控制器。但是我可能错过了一些东西......

1 个答案:

答案 0 :(得分: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(或子类)都是问题所在。