UINavigationBar .items accessor doesn't return the current UINavigationItem

我是研究僧i 提交于 2019-12-24 09:13:23

问题


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.


回答1:


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 ];
}



回答2:


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



来源:https://stackoverflow.com/questions/2154082/uinavigationbar-items-accessor-doesnt-return-the-current-uinavigationitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!