3
votes

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

1

1 Answers

4
votes

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)