I've searched and found several posts concerning how to remove a view from the navigation controller stack. I've implemented one of the techniques found here: How can I remove a view from navigation controller. However, while the view is removed from the stack, the corresponding UINavigationItem is not removed from the navigation bar so my navigation bar gets out of sync with the present view.
Specifically, I have a sequence of views under the control of a Navigation Controller on my storyboard. The opening view allows the user to select between one of two branches to follow. The first branch allows the user to receive and display sensor data coming from an embedded system that the iPhone or iPad is connected to through Wi-Fi. The other path allows the user to log in as an administrator and send some configuration commands to the external embedded system.
If the user chooses to go the administration route they would follow this sequence of screens: Main->Administrator Login->Administration. Once at the Administration view the user can perform several tasks. What I want to do is skip the Administrator Login screen when they back out to Main and then go down the other path (to view the streaming sensor data). In the Administration view I remove the Administrator Login view from the stack using this code
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *navStack = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
NSMutableArray *navBarBrian = [[NSMutableArray alloc] initWithArray: [[[self navigationController] navigationBar] items] ];
UINavigationBar *navBar = [[UINavigationBar alloc] init];
navBar = self.navigationController.navigationBar;
NSLog(@"navStack = %@", navStack);
NSLog(@"navBar = %@", [[self navigationController] navigationBar]);
NSLog(@"navBarItems = %@", [[[self navigationController] navigationBar] items]);
NSLog(@"navBarBrian = %@", navBarBrian);
NSLog(@"navBarList = %@", navBar);
[navStack removeObjectAtIndex: [navStack count] - 2];
[navBarBrian removeObjectAtIndex:[navBarBrian count] - 1];
// self.navigationController.viewControllers = navStack;
[self.navigationController setViewControllers:navStack animated:NO];
// [navBar popNavigationItemAnimated:NO];
// [[[self navigationController] navigationBar] popNavigationItemAnimated:NO];
// [[[self navigationController] navigationBar] setItems:navBarBrian animated:NO];
NSLog(@"navStack = %@", navStack);
NSLog(@"navBar = %@", [[self navigationController] navigationBar]);
NSLog(@"navBarItems = %@", [[[self navigationController] navigationBar] items]);
NSLog(@"navBarBrian = %@", navBarBrian);
}
So when I navigate back from the Administration view, the app does goes to the Main view screen. However the title in the navigation bar still says Administrator Login and has a back button pointing to Main. I have tried popping the Navigation Item off of the Navigation Bar itself but this throws an exception. I can grab the list of Navigation Items, store them in navBarBrian, and also then delete the Navigation Item for the Administrator Login screen from my own navBarBrian array. For example, before I remove the object in navBarBrian I have:
2013-11-06 11:59:25.457 BikeComm[703:a0b] navBarItems = ( "UINavigationItem: 0xa8a4cd0", "UINavigationItem: 0xa8c4f40" )
2013-11-06 11:59:25.457 BikeComm[703:a0b] navBarBrian = ( "UINavigationItem: 0xa8a4cd0", "UINavigationItem: 0xa8c4f40" )
After, removal I have
2013-11-06 11:59:25.458 BikeComm[703:a0b] navBarItems = ( "UINavigationItem: 0xa8a4cd0", "UINavigationItem: 0xa8c4f40" )
2013-11-06 11:59:25.459 BikeComm[703:a0b] navBarBrian = ( "UINavigationItem: 0xa8a4cd0" )
However I am unable to then store the contents of navBarBrian back into the navigation bar using the setItems method as this also throws an exception: "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot call setItems:animated: directly on a UINavigationBar managed by a controller."
So what is the trick to deleting both the view AND the navigation item from their respective stacks?