Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push view controller without stacking

Tags:

ios

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

like image 656
kbtz Avatar asked Feb 15 '23 09:02

kbtz


2 Answers

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];
like image 127
calimarkus Avatar answered Feb 27 '23 12:02

calimarkus


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

like image 38
mattyohe Avatar answered Feb 27 '23 10:02

mattyohe