views:

379

answers:

3

I am breaking the normal paradigm of a UINavigationController, or at least I'm trying to.

We know that the Navigation Controller manages navigation via a stack. If I initialize with root controller A, navigate to B, and then Navigate to C, the Stack will look like - C/B/A.

What I want to do though, is have a button on view C that will show another view, say X, but still keep the Navigation Controller's nav bar in place. Essentially, switch "C" and "X" on the stack. I tried to accomplish that by popping C off and immediately pushing X as shown below, but it didn't work. Am I going down the wrong path here?

-(IBAction)showViewX:(id)sender {
[[self.parentViewController navigationController] popViewControllerAnimated:NO];

XViewController *xViewController = [[XViewController alloc]
  initWithNibName:@"X" 
  bundle:[NSBundle mainBundle]];
[[self.parentViewController navigationController] pushViewController: xViewController animated:YES];
[xViewController release]; 
}
+1  A: 

It might be the case that you really aren't trying to do the kind of navigation that the UINavigationController was designed to support. In your example, how does your user get back from view X to view C?

UINavigationController is intended to allow basic forward/back navigation. If you have more-complex navigation needs, you'll have to build your own navigation system. Take a look at how some of the more-complex Apple applications do their navigation (Mail and Safari) - maybe there's something in there that'll be helpful?

Mark Bessey
To answer your question, "how does your user get back from view X to view C?", they wouldn't be able to. Instead, when they navigate back they would go to B instead of C. (In my case, B has buttons to go to C, X, and other views).
bpapa
+1  A: 

(Looking at the clarifying comment above, I misunderstood the goal of the original question, a better approach would be to manage the display of either View X or View C from within View C itself, switching out as necessary, and not altering the stack at all.)

Since you want to keep the navigation controller bar, but you do not want to be able to go back to 'C' from 'X' (I assume there is some other path out of 'X') Then you could do the following:

self.navigationItem.hidesBackButton = YES;

from within ViewController X, inside the viewDidLoad method.

X is still on the stack, C is still on the stack, but you get the visual representation you wanted.

Otherwise you'll probably need to roll a new NavigationContoller, pop X onto it's stack, and jump to it, leaving your old stack (and ViewController) behind.

mmc
+2  A: 

What you call "view C" is actually "view controller C". Since it is a view controller, you can easily set it up to switch between, say, two views, X and Y. So you just leave C on the stack, don't pop it, and switch between views in C itself. For example, you can set up C to use a toolbar to switch between views.

Felixyz
excellent, didn't think of it that way at all.
bpapa