前言
今天有个小需求,在点击导航条上的返回按钮之前要调用某个API,并弹出UIAlertView来显示,根据用户的选项判断是否是返回还是继续留在当前控制器。举个简单的例子,当点击导航条上的左上角返回按钮时,就调用我们的API来提示是否知道,点击知道则返回,点击不知道则继续留在当前控制器。
那么问题来了,导航自带的右滑返回手势在点击系统的返回按钮时,不会没有办法处理,那是自动的,因此就要想办法改成leftBarButtonItem了,但是使用了leftBarButtonItem就没有了右滑返回手势。
鱼和熊掌不可兼得?笔者自有办法!
笔者尝试写个demo来验证有什么办法可以解决,尝试了以下四种:
- 只在当前controller遵守UIGestureRecognizerDelegate并设置代理为self
- 将UIGestureRecognizerDelegate放在公共基类控制器遵守并设置代理为self,然后子类重写代理方法
- 将UIGestureRecognizerDelegate放在公共导航类HYBNavigationController里遵守,并设置代理为导航类,然后重写push/pop相关的所有方法
- 将UIGestureRecognizerDelegate放在公共导航类HYBNavigationController里遵守,并设置代理为导航类,但是,只遵守-gestureRecognizerShouldBegin:代理方法
方案一(不可行)
方案一:只在当前controller遵守UIGestureRecognizerDelegate并设置代理为self
为什么不可行呢?当想不测试怎么知道呢?光想是很难考虑全面的。于是写了个小demo来测试。
我们在该controller里这样写:

1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 5 [button setTitle:@"返回" forState:UIControlStateNormal]; 6 [button addTarget:self action:@selector(onBack) forControlEvents:UIControlEventTouchUpInside]; 7 [button sizeToFit]; 8 [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 9 UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 10 self.navigationItem.leftBarButtonItem = btnItem; 11 12 // 关键行 13 self.navigationController.interactivePopGestureRecognizer.delegate = self; 14 }
一旦设置了代理为self,那么使用leftBarButtonItem后就可以实现点击回调,而且右滑手势还在。
但是,self.navigationController那可是导航控制器对象的的代理被修改当某个控制器对象了,当这个控制器类被释放后,那么代理就为nil了,如此就再也没有右滑返回手势了。
那么可能有人会想,在-viewDidAppear:里设置代理为self,在-viewDidDisappear:时设置代理成原来的代理对象呢?同样不可以。当A push到B,B push到C,然后从C返回后,代理就不再是最初的导航代理了。
所以,该方案不可行。
方案二(不可行)
方案二:将UIGestureRecognizerDelegate放在公共基类控制器遵守并设置代理为self,然后子类重写代理方法
笔者尝试将UIGestureRecognizerDelegate放在HYBBaseViewControlle里遵守,然后实现代理,默认返回YES,表示支持右滑返回。如果要让某个控制器不支持右滑返回或者在返回前先执行什么操作,可以通过重写此代理方法来实现。
当只在一个控制器里时,这是可以实现的。但是,当这个控制器被释放了以后,代理对象就变成了nil了,因此代理是对于导航条对象的,不属性单个控制器的。
方案三(可行,但复杂)
方案三:将UIGestureRecognizerDelegate放在公共导航类HYBNavigationController里遵守,并设置代理为导航类,然后重写push/pop相关的所有方法。
如实现如何下:

1 // 2 // HYBNavigationController.m 3 // NavRightPanGestureDemo 4 // 5 // Created by huangyibiao on 16/2/22. 6 // Copyright © 2016年 huangyibiao. All rights reserved. 7 // 8 9 #import "HYBNavigationController.h" 10 #import "HYBBaseViewController.h" 11 12 @interface HYBNavigationController () <UIGestureRecognizerDelegate> 13 14 @property (nonatomic, assign) BOOL enableRightGesture; 15 16 @end 17 18 @implementation HYBNavigationController 19 20 21 - (void)viewDidLoad { 22 [super viewDidLoad]; 23 24 self.enableRightGesture = YES; 25 self.interactivePopGestureRecognizer.delegate = self; 26 } 27 28 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { 29 return self.enableRightGesture; 30 } 31 32 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { 33 if ([viewController isKindOfClass:[HYBBaseViewController class]]) { 34 if ([viewController respondsToSelector:@selector(gestureRecognizerShouldBegin)]) { 35 HYBBaseViewController *vc = (HYBBaseViewController *)viewController; 36 self.enableRightGesture = [vc gestureRecognizerShouldBegin]; 37 } 38 } 39 40 [super pushViewController:viewController animated:YES]; 41 } 42 43 - (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated { 44 self.enableRightGesture = YES; 45 return [super popToRootViewControllerAnimated:animated]; 46 } 47 48 - (UIViewController *)popViewControllerAnimated:(BOOL)animated { 49 if (self.viewControllers.count == 1) { 50 self.enableRightGesture = YES; 51 } else { 52 NSUInteger index = self.viewControllers.count - 2; 53 UIViewController *destinationController = [self.viewControllers objectAtIndex:index]; 54 if ([destinationController isKindOfClass:[HYBBaseViewController class]]) { 55 if ([destinationController respondsToSelector:@selector(gestureRecognizerShouldBegin)]) { 56 HYBBaseViewController *vc = (HYBBaseViewController *)destinationController; 57 self.enableRightGesture = [vc gestureRecognizerShouldBegin]; 58 } 59 } 60 } 61 62 return [super popViewControllerAnimated:animated]; 63 } 64 65 - (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated { 66 if (self.viewControllers.count == 1) { 67 self.enableRightGesture = YES; 68 } else { 69 UIViewController *destinationController = viewController; 70 if ([destinationController isKindOfClass:[HYBBaseViewController class]]) { 71 if ([destinationController respondsToSelector:@selector(gestureRecognizerShouldBegin)]) { 72 HYBBaseViewController *vc = (HYBBaseViewController *)destinationController; 73 self.enableRightGesture = [vc gestureRecognizerShouldBegin]; 74 } 75 } 76 } 77 78 return [super popToViewController:viewController animated:animated]; 79 } 80 81 @end
这是通过重写所有的pop/push相关方法,通过判断是否要求支持右滑来设置。然后,我们要让某个控制器类在右滑返回或者点击返回之前,先调用我们的API判断,如下:

1 #import "HYBBController.h" 2 3 @implementation HYBBController 4 5 - (void)viewDidLoad { 6 [super viewDidLoad]; 7 8 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 9 [button setTitle:@"返回" forState:UIControlStateNormal]; 10 [button addTarget:self action:@selector(onBack) forControlEvents:UIControlEventTouchUpInside]; 11 [button sizeToFit]; 12 [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 13 UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 14 self.navigationItem.leftBarButtonItem = btnItem; 15 } 16 17 - (BOOL)gestureRecognizerShouldBegin { 18 [self onBack]; 19 return NO; 20 } 21 22 - (void)onBack { 23 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"标哥的技术博客" 24 message:@"知道博客地址是什么吗?" 25 delegate:self 26 cancelButtonTitle:@"不知道" 27 otherButtonTitles:@"知道", nil]; 28 [alertView show]; 29 } 30 31 32 #pragma mark - UIAlertViewDelegate 33 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 34 if (buttonIndex == 0) { 35 36 } else { 37 if ([self.navigationItem.title isEqualToString:@"VC6"]) { 38 NSUInteger index = self.navigationController.viewControllers.count - 3; 39 UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:index]; 40 [self.navigationController popToViewController:vc animated:YES]; 41 } else { 42 [self.navigationController popViewControllerAnimated:YES]; 43 } 44 } 45 } 46 47 @end
这种方案确实实现了我们的需求。但是,有没有更简单的方案呢?今天可能是眼睛有点困的原因,在研究的时候没有意识到第四种方案。在我准备写这篇文章的时候,我再认识地理了一遍逻辑,发现还有非常简单的一种方案可以实现我的需求。
方案四(可靠,最优)
方案四:将UIGestureRecognizerDelegate放在公共导航类HYBNavigationController里遵守,并设置代理为导航类,但是,只遵守-gestureRecognizerShouldBegin:代理方法。

1 @interface HYBNavigationController () <UIGestureRecognizerDelegate> 2 3 @end 4 5 @implementation HYBNavigationController 6 7 8 - (void)viewDidLoad { 9 [super viewDidLoad]; 10 11 self.interactivePopGestureRecognizer.delegate = self; 12 } 13 14 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { 15 BOOL ok = YES; // 默认为支持右滑反回 16 if ([self.topViewController isKindOfClass:[HYBBaseViewController class]]) { 17 if ([self.topViewController respondsToSelector:@selector(gestureRecognizerShouldBegin)]) { 18 HYBBaseViewController *vc = (HYBBaseViewController *)self.topViewController; 19 ok = [vc gestureRecognizerShouldBegin]; 20 } 21 } 22 23 return ok; 24 } 25 26 @end
使用方法与第三种方案一样,是不是非常地简化了?看来是元宵给我的礼物啊,突然想到这样的办法。以前一直没有研究过interactivePopGestureRecognizer属性,这个属性是iOS7以后才有的,因此在项目中一直不能直接使用leftBarButtonItem处理,除非那个界面不要右滑返回。
现在,一切都明了了,想要使用leftBarButtonItem在公共基类控制器中统一调用API来设置就非常简单了,右滑返回手势也可以正常使用~
还等什么,赶紧试试吧!
最后
如果你所使用的项目也有这样的需求,不防试试吧!笔者提供了demo的,因此可以先下载demo来看看效果哦!经过多次测试,笔者认为这是可行的方案,大家若在使用中出现问题,还请反馈与笔者,我也想了解是什么情况,当然也要找解决方案,共同进步嘛。
源代码
请大家到GITHUB下载吧:CoderJackyHuang