Why does using setViewControllers remove everything from the UINavigationController's nav bar?

我的梦境 提交于 2019-12-23 10:44:15

问题


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];

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

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)?

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.

Any help would be greatly appreciated.


回答1:


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.

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.



来源:https://stackoverflow.com/questions/13362094/why-does-using-setviewcontrollers-remove-everything-from-the-uinavigationcontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!