Pablo
Pablo

Reputation: 29519

Programmatically initialize navigation controller stack

This is the code for the button press:

- (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];
}

This is my storyboard enter image description here

This is exception I got:

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

What I'm trying to achieve is to show the rightmost view controller(in storyboard) when tapping on button. Then I want the back button of navigation controller to take to root view controller, just as I would reach that controller manually.

For that I change to the second tab(because button is on first tab), then I try to initialize Navigation controller. But there is something I probably missing...

Upvotes: 0

Views: 3143

Answers (1)

Darren Findlay
Darren Findlay

Reputation: 2563

Make sure you have your storyboard IDs set on the correct view controllers within storyboards. To me it looks like either

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

Or

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

Is creating a new instance of a UINavigation controller. You could confirm this by using the following

    - (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];
    }

Then check the class names in the debugger. whichever one is UINavigationController (or a subclass) is the one thats the problem.

Upvotes: 1

Related Questions