3
votes

I am making a small app with the following view hierarchy with UINavigationController:

Login -> Options -> three different views

The problem is that I would like to navigate between the last 3 views in the following manner:

1<->2
1<->3
2<->3

i.e. to be able to switch to any view from any other view, which reminds UITabViewController functionality. So, it is not hierarchical, it is any-to-any graph. To switch between views I will use buttons in the navigation bar.

The easiest way for me is to subclass UINavigationController, add properties that correspond to my views and implement methods for switching between these views (using pushViewController and popToRootViewController). These methods will be called from the views for switching (navigating).

However the reference says that UINavigationController is not intended for subclassing. http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html

What do you recommend me to do?

2
I don't think that subclass UINavigationController is a good idea. Besides, I think it's not under NDA. Do you have an idea of what kind of transition you want between the views ?Sébastien Polytech'
The transition I need is just a simple built-in animated transition. The idea is to have these all VC in a stack and then show a VC depending on the button pressed in a current VC.Alexander

2 Answers

0
votes

I'll keep the UINavigationController but instead of using the usual pushViewController:, switch views like this:

NSMutableArray *viewControllers = [self.navigationController.viewControllers mutableCopy];
    // from here you can modify the order of controllers as much as you want
[viewControllers addObject:nextViewController];
[viewControllers removeObject:self];

[self.navigationController setViewControllers:viewControllers animated:YES];

If you don't want how the animation turns out, you can set animated:NO and either enclose setViewControllers: in an [UIView animate...] block, or add your own custom CAAnimation to the navigation controller's layer.

0
votes

Use the below code to add a view controller to a navigation controller,

Navigating From first -> second

SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    NSMutableArray *navigationarray = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
    [navigationarray removeAllObjects];
    [navigationarray addObject:secondView];
    self.navigationController.viewControllers = navigationarray;

Navigating From first -> third

ThirdViewController *thirdView = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    NSMutableArray *navigationarray = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
    [navigationarray removeAllObjects];
    [navigationarray addObject:thirdView];
    self.navigationController.viewControllers = navigationarray;

The above code will removes all the viewControllers from the Navigation Array and places a fresh View Controller

If u want to go to a particular view controller, then use the below code...

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES]

Change the index to ur view controller in the stack.