Search code examples
iosios7uiviewcontrolleruinavigationcontrolleruitabbarcontroller

Push a View Controller in a UINavigationController


I have a tab view controller with a navigation controller. In the first tab item I click on a button in a view that pops up a view with animated: YES.

Then when that view is done I hit another button that dismisses it. Like:

                   [self dismissViewControllerAnimated:NO completion:^{

                        ProfilesViewController *profile = [[ProfilesViewController alloc] init];
                        [self.navigationController pushViewController:profile animated:YES];

                        //SHOW YOUR NEW VIEW CONTROLLER HERE!
                    }];

But everytime this code runs, it dismisses the view, DOES NOT push the profiles controller, and shows the view from the first tab bar item.

How do I push the ProfilesViewController to the screen with a Back arrow?


Solution

  • If you are using dismissViewControllerAnimated to dismiss that means that the VC is presented modally. As such, it doesn't have a navigation controller (so self.navigationController is nil) and thus it can't push anything into the navigation controller.

    You should really add a property to the controller which is a delegate or a completion block which can be used to push the controller from another controller (the one that presents it) to dismiss and push the controller.

    A second option is to pass the navigation controller, it's a similar amount of code to using a block but not so good.

    A crappy option is to use the parentViewController to find the appropriate navigation controller, but that sucks for many reasons.