4
votes

Is there any way to push a view controller to the navigation controller without stacking it?

Wanted behavior (stack representation):

[VC1[VC2]] -> Push VC3 from VC2 -> [VC1[VC3]]

2
I think that is called a replace segue - Rob van der Veer

2 Answers

7
votes

Yeah, just pop the other one before (without animating this one) like this:

[navController popViewControllerAnimated:NO]
[navController pushViewController:VC3 animated:YES]

Or go for option 2, which is more general: Replace the viewControllers property:

NSArray *newControllers = @[VC1, VC3];
[navController setViewControllers:newControllers animated:YES];

or...

NSArray *newControllers = @[navController.viewControllers[0], VC3];
[navController setViewControllers:newControllers animated:YES];
1
votes

I would make use of UINavigationController's method:

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated

That way you can do this:

UINavigationController *navigationController = [self navigationController];
[navigationController setViewControllers:@[navigationController.viewControllers[0], VC3] animated:YES];

This will give you a push animation to the new view controller (VC3).