Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching between sibling views via UINavigationViewController

I am using a NavigationViewController to navigate between master view and detailed views. Now I want to be able to switch to a sibling detail view without first showing the master view and I have tried doing popViewController and pushViewController in the same method or popViewController in the detailed view and then pushViewController in mater's viewDidLoad after popViewController but it won't work - the view just ends up going back to the master view without switching to the detail. Any idea what to do?

The solution suggested here doesn't work as far as I can tell: Switching Content Views Issue

like image 682
Boon Avatar asked Dec 06 '25 05:12

Boon


1 Answers

I've never tried it, but this should work:

// create instance of new view controller
MyViewController *myViewController = [[MyViewController alloc] init];

// get current stack of viewControllers from navigation controller
NSMutableArray *viewControllers = [[self.navigationController viewControllers] mutableCopy];

// replace top view controller in stack
[viewControllers replaceObjectAtIndex:([viewControllers count] - 1) withObject:myViewController];

// set modified stack of view controllers in navigation controller
[self.navigationController setViewControllers:viewControllers animated:YES];

According to the docs, your app will transition to the new view controller with a push animation, and then when back button is clicked it it will be as if the view controller you pushed from was never there. (If you don't want the animation, use animated:NO)

like image 180
jonkroll Avatar answered Dec 07 '25 21:12

jonkroll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!