获取系统导航栏 按钮返回点击 事件

在我们使用系统导航栏的时候,有时当我们 A页面-->B页面-->C页面 之后,我需要直接返回从 C --> A 的时候,我们是需要获取点击事件的。此处我们就需要注意到:

@protocol UINavigationBarDelegate <UIBarPositioningDelegate>
@optional

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item; // called to push. return NO not to.
- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item;    // called at end of animation of push or immediately if not animated
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item;  // same as push methods
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item;

@end

所以,我们可以这块着手啦,但是注意不可以直接在Controller拿来就用哦,否则会返回下面的错误:

Cannot manually set the delegate on a UINavigationBar managed by a controller.

应该怎么办呢,我们需要写一个UINavigationController 的 Category,而且如果我们直接写的话,注意必须写在创建的UINavigationController的地方哦

@implementation UINavigationController (PopBackAction)

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {

    UIViewController * viewController = [self topViewController];
    [viewController.navigationController popToRootViewControllerAnimated:YES];
    return NO;
}

@end

按上述这样,我们就直接改变了点击 back 的事件,但是我们怎样才能更好的运用到我们项目中,参考了BackButtonHandler,写一个 UIViewController 的 Category ,然后再和UINavigationController 的Category 一起。

#import <UIKit/UIKit.h>

@protocol PopBackButtonActionProtocol <NSObject>

@optional
- (BOOL)navigationShouldPopOnBackButton;

@end

@interface UIViewController (PopBackButtonAction)<PopBackButtonActionProtocol>

@end

#import "UIViewController+PopBackButtonAction.h"

// UIViewController Category
@implementation UIViewController (PopBackButtonAction)


@end

// UINavigationController Category 写在一起避免要写两个Category 还要引入头文件的问题
@implementation UINavigationController (ShouldPopOnBackButton)

// 重写
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
    
    // 验证判断 层级
    if([self.viewControllers count] < [navigationBar.items count]) {
        return YES;
    }
    BOOL shouldPop = YES;
    // 代理返回
    UIViewController* viewController = [self topViewController];
    if([viewController respondsToSelector:@selector(navigationShouldPopOnBackButton)]) {
       shouldPop = [viewController navigationShouldPopOnBackButton];
    }
    /**
     *  此处为什么要写这个呢,因为当你 
            1、A --> B --> C ;
            2、C --> A 之后 你再进入 B 之后,B不能返回 A的情况
     */
    if(shouldPop) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self popViewControllerAnimated:YES];
        });
    } else {
        for(UIView *subview in [navigationBar subviews]) {
            if(0. < subview.alpha && subview.alpha < 1.) {
                [UIView animateWithDuration:.25 animations:^{
                    subview.alpha = 1.;
                }];
            }
        }
    }
    return NO;
}

@end

// VC 中的实现, 引入头文件#import "UIViewController+PopBackButtonAction.h" 就 OK 啦
- (BOOL)navigationShouldPopOnBackButton {
    // 实现你所想做的
    [self.navigationController popToRootViewControllerAnimated:YES];
    return NO;
}

确实要注意,那个 shouldPop 处为什么要判断的问题,Setting action for back button in navigation controller。当你使用的时候,我们把其注释掉就知道了。

ps:popAction这个不仅实现了上述问题,还添加了手势返回的效果,可以仔细看看。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容