Why does using setViewControllers remove everything from the UINavigationController navigation bar?

Here is the problem. I am using the following code in a UINavigationController to replace rootViewController . I need to be able to swap rootViewController between multiple screens and allow the user to switch from each screen (which is obviously why I need a Nav controller):

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

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

When I print the score of my viewControllers array, I get 1, the current rootViewController. Should there be something there for the top controller (which I assume is a navigation bar)?

As soon as I use setViewControllers with a new view, everything in my navigation bar disappears. I will add that I have a left and right button, as well as a custom name button, which are all initialized and added to the navigation bar in the init method, and work fine until they disappear on me.

Any help would be greatly appreciated.

+4
source share
1 answer

Since the UINavigationController controls the stack (array) of view controllers, if you must provide it with a stack, it will free and delete the old stack. If you need to add multiple view controllers, you can use -pushViewController:animated:completion: with an animated set for NO for all but the last view controller.

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

+5
source

Source: https://habr.com/ru/post/1445774/


All Articles