views:

357

answers:

2

I'm manually managing a UINavigationBar for my view. The Bar itself and the first UINavigationItem are created in Interface Builder. In my code, based upon various events, I push new navigation items onto the bar and paint the appropriate views as subviews of the main view. It all seems fine, but when I select the navbar back button, two items are lopped off the items stack, rather than 1 as I would have expected. To test, I set my controller as the delegate for the bar and trap 2 delegate methods:

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item;

and

- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item;

I found right before the pop, the items in the nav bar is correct, for example, 3. In the second method, just after the pop, the items is 1, even though the item popped is the last item - somehow the middle item is missing. I'm stumped as to how to debug this and would appreciate any ideas.

Note that I'm not using a navigation controller for various reasons.

A: 

"I'm manually managing a UINavigationBar for my view."

Don't do that. ;-)

Seriously. I can't think of a good reason to manage it yourself and if you're not very careful you'll run into issues that make the whole app unmanageable. So why not just create a UINavigationController in your root view that is connected to your XIB? What problem are you trying to overcome by managing the nav bar yourself?

As to the problem you're having--you get two pops because you are doing something in shouldPopItem that is telling it to pop again. That is an effect of trying to manage the nav bar yourself. Keep in mind that the shouldPopItem and didPopItem only relate to the nav bar and not the navigation controller and views. When you pop a view controller with [[self navigationController] popViewControllerAnimated:YES]; it will call shouldPopItem. Since you are overriding that, you have to manage everything.

Matt Long
Yes, thanks. I was thinking that perhaps something was calling didPopItem and shouldPopItem twice, which is why I set up the delegate methods so I could trap these. They're only called once. At the time it seemed easier to extend my viewcontroller than to implement the navigation controller. And given that IB gives you the tools to set this up, I figured it would be OK. Plus, I'm fairly new to iphone dev and like to understand the framework mechanics. I've worked my way around it now, and chalked this up to yet another thing with the environment that has evaded my understanding...
LeftHem
A: 

I would start by placing an NSLog() statement at the beginning of your shouldPopItem and didPopItem methods - this will give you an idea of how many times they're getting called, and when. Next, you can log the size of the view controller stack at various places throughout your app, so you can watch it grow and shrink. If neither of those work, just start placing breakpoints and checking stack traces/view controller stack size manually.

Tim
Thanks - I wasn't very clean in my post - this is exactly what I was doing - setting breakpoints and viewing the size of the stack and what's on the stack. Very strange.
LeftHem