0

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?

4

1 回答 1

0

有点困惑你想在这里做什么......但你的所作所为似乎很尴尬。通常,当您遇到此类问题时,您的 UI 存在缺陷。

登录屏幕通常是模态视图,而不是导航控制器的一部分。你正在做的事情,你永远不应该做。

也许从你的主屏幕让登录成为一个模式,并为你的登录屏幕创建一个代理,让你的主监听。登录后,调用委托并关闭模式。然后在 Main 上的委托侦听器方法中,如果登录成功,则推送 Admin 屏幕.. 然后当您回击时,您不必绕过登录。

如果这是有道理的。

我还建议阅读iOS 人机界面指南,以了解哪些 UI 元素最适合哪些情况。

于 2013-11-06T19:51:13.930 回答