您的位置:首页 > 其它

Custom backBarButtonItem

2015-04-15 11:45 295 查看


我想得到的效果:

当用户点击
backBarButtonItem
的时候,在pop前,我想处理一些逻辑来判断是否pop。

并且我想要保留
backBarButtonItem
的'<'。


为什么得不到这种效果

backBarButtonItem
绑定事件会被忽略,
UINavigatonController
自动为其绑定事件,只做POP动作。There
is nothing we can do.
使用
leftBarButtonItem
可以绑定事件,但是'<'就不存在了,当然可以定制View来达到效果,但是如果需要兼容iOS6则需要更多的工作(iOS6的
backBarButtonItem
试样与iOS7不同),而且谁也不会知道在iOS9中,会出现什么新设计。(在iOS9快释出的时候还适配iOS6?其实只是强行找个写这篇文字的理由
:])


我试过的方法:

Add target on
backBarButtonItem
. Failed.
Set
leftBarButtonItem
with charactor '<'. (All kind of
'<' I could find in Characters Viewer) 用一个字符'<'来显示
backBarButtonItem
的'<'效果,比如'↺'和'√'。这样就不用自己绘制或贴图了。
Set
leftItemsSupplementBackButton
to
YES
.
该属性使
backBarButtonItem
leftBarButtonItem
同时显示,
leftBarButtonItem
backBarButtonItem
的右边,于是我就想让
backBarButtonItem
只显示一个'<',让
leftBarButtonItem
显示文字,并disable
backBarButtonItem
不就可以了?但是剧本不是我写的。set
"" to
backBarButtonItem
and set "Back" to
leftBarButtonItem
,
but there is a gap between '<' and 'Back'.


最终解决方案:

Subclass
UINavigatonController
, override
-
 (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item

Category
UINavigatonController
, expose super's
-
 (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item



解释如下:

UINavigationBarDelegate
定义了一些方法来控制POP和PUSH行为:
[code]navigationBar:shouldPushItem:
navigationBar:didPushItem:
navigationBar:shouldPopItem:
navigationBar:didPopItem:


这里我们利用了
navigationBar:shouldPopItem:
,如果该方法返回NO,则不POP。

因此我们创建
UINavigatonController
的子类,来定制
navigationBar:shouldPopItem:
的逻辑。

这里有个小地方要注意,就是我们不需要设置delegate,
UINavigatonController
会自动将包含的
UINavigationBar
的delegate指向自己。

子类的
navigationBar:shouldPopItem:
我们希望在处理完定制的逻辑后调用父类的该方法完成POP,但是父类
UINavigatonController
并没有把
navigationBar:shouldPopItem:
作为接口暴露出来,因此我们需要一点
Category
的小技巧来为父类创建
navigationBar:shouldPopItem:
的接口。


代码如下:


WFNavigationController.h文件

[code]@protocol WFNavigationControllerDelegate <NSObject>
@optional
- (BOOL)controllerWillPopHandler;
@end

@interface  WFNavigationController : UINavigationController
@end


WFNavigationController.m文件

[code]@interface UINavigationController(UINavigationControllerNeedshouldPopItem)
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item;
@end

@implementation UINavigationController(UINavigationControllerNeedshouldPopItem)
@end
// 以上几行就是使用Category使UINavigationController将其实现的navigationBar:shouldPopItem:暴露出来,
// 让我们定制的子类可以调用

@interface WFNavigationController() <UINavigationBarDelegate>
@end

@implementation WFNavigationController

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
    UIViewController *vc = self.topViewController;
    if ([vc respondsToSelector:@selector(controllerWillPopHandler)])
    {
        if ([vc performSelector:@selector(controllerWillPopHandler)])
        {
            return [super navigationBar:navigationBar shouldPopItem:item];
        }
        else
        {
            return NO;
        }
    }
    else
    {
        return [super navigationBar:navigationBar shouldPopItem:item];
    }
}

@end


navigation controller栈顶的vc,遵循
WFNavigationControllerDelegate
协议,实现
-
 (BOOL)controllerWillPopHandler
方法即可。


推荐使用block而不是delegate

我在这里使用了delegate而不是block,其实是在偷懒,block是更好的方式,可以让你的代码更易阅读。

因为相关逻辑都放在一起,而不是像使用委托这样到处散落。

在该场景下,还有个推荐使用block的更重要的原因:
NavigationController
的push和pop过程中,
topViewController
可能不是你预期的那个VC
。block可以方便的加载,卸载。

由于
topViewController
的不稳定性,所以这篇文字介绍的方法不是最好的。以后有时间再寻找下别的方式。

考虑以下场景:
[code]A -> B -> C
A创建并 push B,B创建并 push C。


B需要在pop前进行逻辑判断,所以B遵循协议。

这种场景下,有两个地方会触发B实现的委托:

B点击
backBarButtonItem
返回A,这是我们期望的。
C手动
[self.navigationController popViewControllerAnimated:YES];
比如点击
rightBarButtonItem
返回B。这里也会触发!

也就是说
backBarButtonItem
时获取到的
topViewController
是pop前VC,而
[self.navigationController
 popViewControllerAnimated:YES];
获取到的是pop后VC。这种两种路径的设计也蛮“有意思”。

visibleViewController
viewControllers.lastObject
也一样。

这里我没有深入下去,而是在view life circle中加载,卸载popBlock。或者使用delegate外加一个Bool变量在view life circle中启用,禁用委托。

以上解决方案不是很理想,有时间我会再研究整理,看有没有更好的方法。


如果没看懂的话,请留言,我可以写个demo。


以下可以选择性适当忽略:

禁止pop后,< Back中的<会置灰,文字Back却不会(可能又是Apple Inc.的小Bug)。

解决方法很简单:在vc中调用以下两句代码,两句,嗯。
[code][self.navigationController setNavigationBarHidden:YES animated:YES];
[self.navigationController setNavigationBarHidden:NO animated:YES];


比如,用户点击
backBarButtonItem
时,我提示用户是否继续离开,如果用户选择OKPOP离开,如果用户选择NO则留在本页并执行上面两句,使<Back中的<恢复正常颜色。

感谢大家阅读完这篇文字:]

文章转自:这里
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: