Possible to switch out View Controllers managed by a UINavigationController?

Possible to switch out View Controllers managed by a UINavigationController?

顾挽 发布于 2021-11-28 字数 1039 浏览 878 回复 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];  
}

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一桥轻雨一伞开 2022-06-07 3 楼

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

-残月青衣踏尘吟 2022-06-07 2 楼

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?

长不大的小祸害 2022-06-07 1 楼

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.