views:

486

answers:

2

I'm having a problem where my UINavigationBar isn't popping it's items when the UINavigationController pops a view controller.

I have had a look at the stacks observed the following:

  • The expected number of navigation controllers allocated (checked in instruments)
  • There are the correct number of controllers on the stack. (debug logging)
  • The navigation items aren't being popped. (debug logging)

Here's what I did.

I popped some log statements into the viewDidLoad method after calling super.

NSLog(@"%@", [self navigationController]);
NSLog(@"%@", [[self navigationController] viewControllers]);
NSLog(@"%@", [[self navigationController] navigationBar]);
NSLog(@"%@", [[[self navigationController] navigationBar] items]);

After the initial view is loaded.

2009-10-09 16:42:51.706 Bob[11657:207] <UINavigationController: 0x243bb0>
2009-10-09 16:42:51.720 Bob[11657:207] (
    <MediaBrowser: 0x2354c0>
)
2009-10-09 16:42:51.742 Bob[11657:207] <UINavigationBar: 0x243e50; frame = (0 20; 320 44); autoresize = W; layer = <CALayer: 0x243ed0>>
2009-10-09 16:42:51.758 Bob[11657:207] (
    <UINavigationItem: 0x242d60>
)

All looks good. Push the second controller.

2009-10-09 16:43:11.800 Bob[11657:207] -[MediaBrowser beginLoading] [Line 261] 
2009-10-09 16:43:12.320 Bob[11657:207] -[MediaBrowser loadingComplete] [Line 269] 
2009-10-09 16:43:12.587 Bob[11657:207] <UINavigationController: 0x243bb0>
2009-10-09 16:43:12.654 Bob[11657:207] (
    <MediaBrowser: 0x2354c0>,
    <MediaBrowser: 0x2c1cc0>
)
2009-10-09 16:43:12.685 Bob[11657:207] <UINavigationBar: 0x243e50; frame = (0 20; 320 44); autoresize = W; layer = <CALayer: 0x243ed0>>
2009-10-09 16:43:12.734 Bob[11657:207] (
    <UINavigationItem: 0x242d60>,
    <UINavigationItem: 0x2c63b0>
)

Again nothing unexpected here. Push the third controller.

2009-10-09 16:43:19.934 Bob[11657:207] -[MediaBrowser beginLoading] [Line 261] 
2009-10-09 16:43:20.388 Bob[11657:207] -[MediaBrowser loadingComplete] [Line 269] 
2009-10-09 16:43:20.928 Bob[11657:207] <UINavigationController: 0x243bb0>
2009-10-09 16:43:20.962 Bob[11657:207] (
    <MediaBrowser: 0x2354c0>,
    <MediaBrowser: 0x2c1cc0>,
    <MediaBrowser: 0x4027e20>
)
2009-10-09 16:43:21.003 Bob[11657:207] <UINavigationBar: 0x243e50; frame = (0 20; 320 44); autoresize = W; layer = <CALayer: 0x243ed0>>
2009-10-09 16:43:21.039 Bob[11657:207] (
    <UINavigationItem: 0x242d60>,
    <UINavigationItem: 0x2c63b0>,
    <UINavigationItem: 0x4028880>
)

All good. Now lets pop a controller

2009-10-09 16:43:26.935 Bob[11657:207] <UINavigationController: 0x243bb0>
2009-10-09 16:43:26.945 Bob[11657:207] (
    <MediaBrowser: 0x2354c0>,
    <MediaBrowser: 0x2c1cc0>
)
2009-10-09 16:43:26.964 Bob[11657:207] <UINavigationBar: 0x243e50; frame = (0 20; 320 44); autoresize = W; layer = <CALayer: 0x243ed0>>
2009-10-09 16:43:26.977 Bob[11657:207] (
    <UINavigationItem: 0x242d60>,
    <UINavigationItem: 0x2c63b0>,
    <UINavigationItem: 0x4028880>
)

Humm. Something is going amiss. The controller has popped but the navigation item is still there. For giggles lets push again.

009-10-09 16:44:05.878 Bob[11657:207] <UINavigationController: 0x243bb0>
2009-10-09 16:44:05.908 Bob[11657:207] (
    <MediaBrowser: 0x2354c0>,
    <MediaBrowser: 0x2c1cc0>,
    <MediaBrowser: 0x407eb00>
)
2009-10-09 16:44:05.951 Bob[11657:207] <UINavigationBar: 0x243e50; frame = (0 20; 320 44); autoresize = W; layer = <CALayer: 0x243ed0>>
2009-10-09 16:44:05.996 Bob[11657:207] (
    <UINavigationItem: 0x242d60>,
    <UINavigationItem: 0x2c63b0>,
    <UINavigationItem: 0x4028880>,
    <UINavigationItem: 0x407f280>
)

The controller stack looks good, the navigation item stack is in lots of trouble. Lets pop one last time.

2009-10-09 16:44:17.770 Bob[11657:207] <UINavigationController: 0x243bb0>
2009-10-09 16:44:17.776 Bob[11657:207] (
    <MediaBrowser: 0x2354c0>,
    <MediaBrowser: 0x2c1cc0>
)
2009-10-09 16:44:17.785 Bob[11657:207] <UINavigationBar: 0x243e50; frame = (0 20; 320 44); autoresize = W; layer = <CALayer: 0x243ed0>>
2009-10-09 16:44:17.793 Bob[11657:207] (
    <UINavigationItem: 0x242d60>,
    <UINavigationItem: 0x2c63b0>,
    <UINavigationItem: 0x4028880>,
    <UINavigationItem: 0x407f280>
)

So the controller stack is fine. Navigation items on the other hand have some issues.

I'm stumped. I'm sure I'm doing something odd/weird/wrong but damn me if I can see it.The only manipulation i'm going is preparing (via my controllers navigationItem property) a custom rightBarItem.

If you got this far, thanks. Thoughts on where I might go from here would be appreciated.

A: 

What is your code to add the custom right bar item? What method is it in?

Kendall Helmstetter Gelner
the right bar item is added in the init method for the controller.
Kevin
+2  A: 

Nailed it.

So it turns out that I was doing something stupid.

I had added a pop method in an internal category to NSMutableArray. That method wasn't popping correctly. UINavigationBar must also add a pop method to NSMutableArray. My buggy one was winning and breaking the pop of the UIBarItem.

Kevin