cvsguimaraes
cvsguimaraes

Reputation: 13240

Push view controller without stacking

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]]

Upvotes: 4

Views: 2454

Answers (2)

calimarkus
calimarkus

Reputation: 9977

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

Upvotes: 7

mattyohe
mattyohe

Reputation: 1814

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).

Upvotes: 1

Related Questions