拦截导航栏返回事件

1 分钟阅读

注意!转载请注明出处作者,谢谢

iOS拦截导航栏返回按钮事件的正确方式

点击导航栏的返回按钮时,由于需求,如文本编辑页面需要询问是否保存文件再退出,所以我们要对导航返回事件进行拦截判断如何退出页面。我觉得为 UIViewController 添加 category 是最好的方法了,具体如下:

新建 UIViewController 的 category

添加一个协议,调用协议方法以执行导航返回时的任务。

@protocol BackButtonHandlerProtocol <NSObject>
@optional
// 重写下面的方法以拦截导航栏返回按钮点击事件,返回 YES 则 pop,NO 则不 pop
-(BOOL)navigationShouldPopOnBackButton;
@end

@interface UIViewController (MKNavigationPopBack) <BackButtonHandlerProtocol>

@end
#import "UIViewController+MKNavigationPopBack.h"

@implementation UIViewController (MKNavigationPopBack)

@end

@implementation UINavigationController (ShouldPopOnBackButton)

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
    
    if([self.viewControllers count] < [navigationBar.items count]) {
        return YES;
    }
    
    BOOL shouldPop = YES;
    UIViewController* vc = [self topViewController];
    if([vc respondsToSelector:@selector(navigationShouldPopOnBackButton)]) {
        shouldPop = [vc navigationShouldPopOnBackButton];
    }
    
    if(shouldPop) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self popViewControllerAnimated:YES];
        });
    } else {
        // 取消 pop 后,复原返回按钮的状态
        for(UIView *subview in [navigationBar subviews]) {
            if(0. < subview.alpha && subview.alpha < 1.) {
                [UIView animateWithDuration:.25 animations:^{
                    subview.alpha = 1.;
                }];
            }
        }
    }
    return NO;
}
@end

使用方法

#import "UIViewController+MKNavigationPopBack.h"

//拦截导航栏返回事件
-(BOOL)navigationShouldPopOnBackButton {
    kWeakSelf(self)
    //TODO: 保存草稿箱
    UIAlertController *ctrl = [UIAlertController alertControllerWithTitle: @"提示" message: @"邮件还没发送,需要保存草稿箱吗?" preferredStyle: UIAlertControllerStyleAlert];
    [ctrl addAction: [UIAlertAction actionWithTitle: @"保存" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        kStrongSelf(self)
        [self.navigationController popViewControllerAnimated: YES];
    }]];
    [ctrl addAction: [UIAlertAction actionWithTitle: @"不保存" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        kStrongSelf(self)
        [self.navigationController popViewControllerAnimated: YES];
    }]];
    [ctrl addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler: nil]];

    [self presentViewController: ctrl animated: YES completion: nil];
    return NO;
}

更新: