xcode 为什么使用 setViewControllers 会从 UINavigationController 的导航栏中删除所有内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13362094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does using setViewControllers remove everything from the UINavigationController's nav bar?
提问by r0ddy
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):
这就是问题所在。我在 a 中使用以下代码UINavigationController
来交换rootViewController
. 我需要能够rootViewController
在多个不同的屏幕之间交换,并允许用户从每个屏幕向下钻取(这显然是我需要导航控制器的原因):
SomeViewController *tmp = [[SomeViewController alloc] initWithNibName:@"SomeViewController" bundle:nil];
NSArray * viewControllers = [self viewControllers];
NSArray * newViewControllers = [NSArray arrayWithObjects:tmp, nil];
[self setViewControllers:newViewControllers];
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."
现在在阅读类参考时,UINavigationController
我看到“viewControllers”属性说:“根视图控制器位于数组中的索引 0,后视图控制器位于索引 n-2,顶部控制器是在索引 n-1 处,其中 n 是数组中的项目数。”
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)?
当我在那里打印我的 viewControllers 数组的计数时,我得到 1,即当前的 rootViewController。顶部控制器(我认为是导航栏)中不应该有什么东西吗?
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.
一旦我使用setViewControllers
新视图,导航栏中的所有内容都会消失。我要补充一点,我有一个左右按钮以及一个自定义标题按钮,它们都已初始化并添加到 init 方法中的导航栏,并且一直工作正常,直到它们消失在我身上。
Any help would be greatly appreciated.
任何帮助将不胜感激。
回答by CodaFi
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.
因为 UINavigationController 管理视图控制器的堆栈(数组),如果您要为其提供一个堆栈,它将释放并删除它的旧堆栈。如果您需要添加多个视图控制器,您可以将-pushViewController:animated:completion:
动画设置为 NO 用于除最后一个视图控制器之外的所有视图控制器。
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.
因为你给它的堆栈只包含一个视图控制器,-count
会打印 1,而且它实际上是顶视图控制器。