UINavigationBar似乎在“后”弹出2个项目 [英] UINavigationBar seems to pop 2 items off stack on "back"

查看:83
本文介绍了UINavigationBar似乎在“后”弹出2个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我手动管理UINavigationBar以获得我的视图。 Bar自身和第一个UINavigationItem是在Interface Builder中创建的。在我的代码中,基于各种事件,我将新的导航项目推到栏上,并将适当的视图绘制为主视图的子视图。这一切似乎都很好,但当我选择导航条后退按钮,两个项目被丢弃的项目堆栈,而不是我所期望的1。要测试,我设置我的控制器作为bar和trap的委托方式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; 

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

我在弹出之前找到了,导航栏中的项目是正确的,例如3。在第二种方法中,刚刚弹出后,项目是1,即使弹出的项目是最后一个项目 - 不知何故中间项目丢失。

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.

推荐答案

我不知道为什么UINavigationBar的行为像这样,但我有一个棘手的解决方案。

I don't know why UINavigationBar behaves like this, but I have a tricky solution.

- (BOOL)navigationBar:(UINavigationBar *)navigationBar
    shouldPopItem:(UINavigationItem *)item
{
    NSMutableArray *items = navigationBar.items.mutableCopy;
    [items removeObject:item];

    // Now var 'items' contains the proper instances of UINavigationItem. To be exact, one more than the instances in 'navigationBar.items'.

    return YES;
}

这篇关于UINavigationBar似乎在“后”弹出2个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆