为什么使用 setViewControllers 会从 UINavigationController 的导航栏中删除所有内容? [英] Why does using setViewControllers remove everything from the UINavigationController's nav bar?

查看:139
本文介绍了为什么使用 setViewControllers 会从 UINavigationController 的导航栏中删除所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题来了.我在 UINavigationController 中使用以下代码来交换 rootViewController.我需要能够在多个不同的屏幕之间交换 rootViewController 并允许用户从每个屏幕向下钻取(这显然是我需要导航控制器的原因):

Here's the issue. I use the following code in a UINavigationController to swap the rootViewController. I need to be able to swap the rootViewController between a number of different screens and allow the user to drill down from each screen (which is obviously why I need the Nav Controller):

SomeViewController *tmp = [[SomeViewController alloc] initWithNibName:@"SomeViewController" bundle:nil];
NSArray * viewControllers = [self viewControllers];
NSArray * newViewControllers = [NSArray arrayWithObjects:tmp, nil];
[self setViewControllers:newViewControllers];

现在在阅读 UINavigationController 的类引用时,我看到viewControllers"属性说:根视图控制器位于数组中的索引 0,后视图控制器是在索引 n-2 处,顶部控制器在索引 n-1 处,其中 n 是数组中的项数."

Now in reading through the class reference for the UINavigationController I saw that for the "viewControllers" property it says: "the root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array."

当我在那里打印我的 viewControllers 数组的计数时,我得到 1,即当前的 rootViewController.顶部控制器(我假设它是导航栏)不应该在那里吗?

When I print a count of my viewControllers array there, I get 1, the current rootViewController. Should there not be something in there for the top controller (which I assume to be the nav bar)?

一旦我在新视图中使用 setViewControllers,导航栏中的所有内容都会消失.我要补充一点,我有一个左右按钮以及一个自定义标题按钮,它们都已初始化并添加到 init 方法中的导航栏,并且一直工作正常,直到它们消失在我身上.

As soon as I use setViewControllers with the new view, everything in my nav bar disappears. I'll add that I have a left and right button as well as a custom title button which are all initialized and added to the nav bar in the init method and have been working fine until they disappeared on me.

任何帮助将不胜感激.

推荐答案

因为 UINavigationController 管理着一个视图控制器的堆栈(数组),如果你为它提供一个堆栈,它会释放并移除它的旧堆栈.如果您需要添加多个视图控制器,您可以使用 -pushViewController:animated:completion: 并将动画设置为 NO,但最后一个视图控制器除外.

Because UINavigationController manages a stack (array) of view controllers, if you were to provide it a stack, it would deallocate and remove it's old stack. If you need to add multiple view controllers, you can use -pushViewController:animated:completion: with animated set to NO for all but the last view controller.

因为你给它的堆栈只包含一个视图控制器,-count 会打印 1,它实际上是顶视图控制器.

Because the stack you gave it contained only one view controller, -count would print 1, and it would, in fact, be the top view controller.

这篇关于为什么使用 setViewControllers 会从 UINavigationController 的导航栏中删除所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆