views:

294

answers:

2

In my UINavigationBar: didPopItem: function the navigation bar's _itemStack has n items in it, shown in the debugger, but the .items accessor function returns an array with n-1 items in it, missing the current navigation item, which is what I want to check. backItem returns the n-2 item instead of the n-1, etc. The didPopItem item is the 'n+1' item so that doesn't help either.

How do I access the current UINavigationItem?

I'm using the iPhone 3.0 SDK.

A: 

Hi,

The callback didPopItem happens after the item has been removed so the current navigation item probably isn't the one you are expecting!

Try putting your code into the shouldPopItem delegate method instead - this gets called just before an item is removed.

i.e. You have 3 items on your stack, A, B and C.

When you remove C, this is what happens :

  1. shouldPopItem is called with items being the array [A, B, C]
  2. C is removed
  3. didPopItem is called with the items array being [A, B]

If you want to keep the item that is going to be removed, store a reference to it in your shouldPopItem method so it's still around when the didPopItem method gets called.

Hope that helps,

S

deanWombourne
But my problem is that .items just contains [A] and not [A, B]. `didPopItem` is C. B is what I want to look at, but I can't see it.But what I might do is note B in `shouldPopItem` so I can check it in `didPopItem`. I'm trying to decide if I should hide the toolbar.
NPAssoc
Oh, that's weird. What does topItem contain?
deanWombourne
Well `topItem` contains A. `backItem` contains nil, as if the stack is one short. As I said, the debugger shows [A, B], but `.items` just shows [A].But even weirder is that just adding a `shouldPopItem` delegate that returns `YES` breaks the application. The Navigation Bar stops popping the content view. The bar pops but not the view. (I spend more time getting around Cocoa quirks like this than writing my application. `didPopItem` doesn't even get called if I am in landscape mode but `didPushItem` does.--another stumper. Should I go to SDK 3.1?)
NPAssoc
Trying 3.1 is probably a good idea. Do you have some example code?
deanWombourne
- (BOOL)navigationBar:(UINavigationBar *)nBar shouldPopItem:(UINavigationItem *)item { theCItem = nBar.topItem; return YES; } - (void)navigationBar:(UINavigationBar *)nBar didPopItem:(UINavigationItem *)item { bool hideBar = true; // UINavigationItem *cItem = [nBar.items objectAtIndex:(nBar.items.count - 1)]; // Doesn't work UINavigationItem *cItem = theCItem; if ([cItem.title compare:@"@ Home"] == NSOrderedSame) hideBar = false; if ([cItem.title compare:@"Ingredients"] == NSOrderedSame) hideBar = false; [self setToolbarHidden:hideBar animated:true]; }
NPAssoc
[I don't know a better way to post code as a comment.]
NPAssoc
A: 

It seems that the "items" property will be adjusted after returning RunLoop. So try like this.

-(void)XXX:(UINavigationBar*)nBar {
    UINavigationItem *cItem = [nBar.items objectAtIndex:(nBar.items.count - 1)];
}

-(void)navigationBar:(UINavigationBar *)nBar didPopItem:(UINavigationItem *)item {
    [ self performSelector:@selector(XXX) withObject:nBar afterDelay:0 ];
}
Satachito
Very clever. Something very few people would think of. Thanks for the response. I'll give it a try.
NPAssoc