Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 602
r0ddy Avatar asked Nov 13 '12 14:11

r0ddy


1 Answers

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.

like image 72
CodaFi Avatar answered Nov 15 '22 11:11

CodaFi