背景:有時(shí)候我們會(huì)遇到需要在用戶點(diǎn)擊返回按鈕pop之前進(jìn)行攔截速种,增加一些業(yè)務(wù)邏輯,如彈出警告彈框詢問用戶是否確實(shí)要返回等配阵。
實(shí)現(xiàn)代碼
- 1、創(chuàng)建一個(gè)協(xié)議MZCustomProtocol
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@protocol MZCustomProtocol <NSObject>
@optional
/// 點(diǎn)擊返回按鈕的時(shí)候, 需要增加額外操作的時(shí)候, 在當(dāng)前控制器里實(shí)現(xiàn)該方法
- (void)onClickBackButtonWithAdditionalOperating:(UIBarButtonItem *)backItem;
@end
NS_ASSUME_NONNULL_END
- 2救拉、自定義導(dǎo)航控制器MZBaseNavVC
#import "MZBaseNavVC.h"
#import "MZCustomProtocol.h"
@interface MZBaseNavVC ()<UIGestureRecognizerDelegate>
@end
@implementation MZBaseNavVC
- (void)viewDidLoad {
[super viewDidLoad];
self.interactivePopGestureRecognizer.delegate = self;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
// 實(shí)現(xiàn)了指定方法的控制器, 就不允許側(cè)滑返回
if ([self.viewControllers.lastObject respondsToSelector:@selector(onClickBackButtonWithAdditionalOperating:)]) {
return NO;;
}
return self.childViewControllers.count > 1;
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (self.childViewControllers.count > 0) {
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
back.frame = CGRectMake(0, 0, 44, 44);
[back setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
[back addTarget:self action:@selector(onClickBackButton:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:back];
viewController.navigationItem.leftBarButtonItem = backItem;
self.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
}
- (void)onClickBackButton:(id)sender {
// 實(shí)現(xiàn)了指定方法的控制器, 就走當(dāng)前控制器里實(shí)現(xiàn)的方法
if ([self.viewControllers.lastObject respondsToSelector:@selector(onClickBackButtonWithAdditionalOperating:)]) {
[(id<MZCustomProtocol>)self.viewControllers.lastObject onClickBackButtonWithAdditionalOperating:sender];
} else {
// If the view controller at the top of the stack is the root view controller, this method does nothing.
[self.navigationController popViewControllerAnimated:YES];
}
}
@end
- 3亿絮、在需要攔截返回按鈕的控制器里,實(shí)現(xiàn)對(duì)應(yīng)的方法
onClickBackButtonWithAdditionalOperating:
#import "DetailViewController.h"
#import "MZCustomProtocol.h"
@interface DetailViewController ()<MZCustomProtocol>
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)onClickBackButtonWithAdditionalOperating:(UIBarButtonItem *)backItem {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"溫馨提示" message:@"返回后數(shù)據(jù)將不保存" preferredStyle:UIAlertControllerStyleAlert];
__weak typeof(self) weakSelf = self;
[alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
[alertVC addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
[weakSelf.navigationController popViewControllerAnimated:YES];
}]];
[self presentViewController:alertVC animated:YES completion:nil];
}
@end