iOS系统右滑返回全局控制方案

移动开发 iOS
今天有个小需求,在点击导航条上的返回按钮之前要调用某个API,并弹出UIAlertView来显示,根据用户的选项判断是否是返回还是继续留在当前控制器。举个简单的例子,当点击导航条上的左上角返回按钮时,就调用我们的API来提示是否知道,点击知道则返回,点击不知道则继续留在当前控制器。

前言

今天有个小需求,在点击导航条上的返回按钮之前要调用某个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.  
  3.   [super viewDidLoad]; 
  4.  
  5.   
  6.  
  7.     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
  8.  
  9.   [button setTitle:@"返回" forState:UIControlStateNormal]; 
  10.  
  11.   [button addTarget:self action:@selector(onBack) forControlEvents:UIControlEventTouchUpInside]; 
  12.  
  13.   [button sizeToFit]; 
  14.  
  15.   [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 
  16.  
  17.   UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 
  18.  
  19.   self.navigationItem.leftBarButtonItem = btnItem; 
  20.  
  21.   
  22.  
  23.   // 关键行 
  24.  
  25.   self.navigationController.interactivePopGestureRecognizer.delegate = self; 
  26.  
  27.  

一旦设置了代理为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. //  HYBNavigationController.m 
  2.  
  3. //  NavRightPanGestureDemo 
  4.  
  5. // 
  6.  
  7. //  Created by huangyibiao on 16/2/22. 
  8.  
  9. //  Copyright © 2016年 huangyibiao. All rights reserved. 
  10.  
  11. // 
  12.  
  13.   
  14.  
  15. #import "HYBNavigationController.h" 
  16.  
  17. #import "HYBBaseViewController.h" 
  18.  
  19.   
  20.  
  21. @interface HYBNavigationController () 
  22.  
  23.   
  24.  
  25. @property (nonatomic, assign) BOOL enableRightGesture; 
  26.  
  27.   
  28.  
  29. @end 
  30.  
  31.   
  32.  
  33. @implementation HYBNavigationController 
  34.  
  35.   
  36.  
  37. - (void)viewDidLoad { 
  38.  
  39.   [super viewDidLoad]; 
  40.  
  41.   
  42.  
  43.   self.enableRightGesture = YES; 
  44.  
  45.   self.interactivePopGestureRecognizer.delegate = self; 
  46.  
  47.  
  48.   
  49.  
  50. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { 
  51.  
  52.   return self.enableRightGesture; 
  53.  
  54.  
  55.   
  56.  
  57. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { 
  58.  
  59.   if ([viewController isKindOfClass:[HYBBaseViewController class]]) { 
  60.  
  61.     if ([viewController respondsToSelector:@selector(gestureRecognizerShouldBegin)]) { 
  62.  
  63.       HYBBaseViewController *vc = (HYBBaseViewController *)viewController; 
  64.  
  65.       self.enableRightGesture = [vc gestureRecognizerShouldBegin]; 
  66.  
  67.     } 
  68.  
  69.   } 
  70.  
  71.   
  72.  
  73.   [super pushViewController:viewController animated:YES]; 
  74.  
  75.  
  76.   
  77.  
  78. - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated { 
  79.  
  80.      self.enableRightGesture = YES; 
  81.  
  82.   return [super popToRootViewControllerAnimated:animated]; 
  83.  
  84.  
  85.   
  86.  
  87. - (UIViewController *)popViewControllerAnimated:(BOOL)animated { 
  88.  
  89.   if (self.viewControllers.count == 1) { 
  90.  
  91.     self.enableRightGesture = YES; 
  92.  
  93.   } else { 
  94.  
  95.     NSUInteger index = self.viewControllers.count - 2; 
  96.  
  97.     UIViewController *destinationController = [self.viewControllers objectAtIndex:index]; 
  98.  
  99.     if ([destinationController isKindOfClass:[HYBBaseViewController class]]) { 
  100.  
  101.       if ([destinationController respondsToSelector:@selector(gestureRecognizerShouldBegin)]) { 
  102.  
  103.         HYBBaseViewController *vc = (HYBBaseViewController *)destinationController; 
  104.  
  105.         self.enableRightGesture = [vc gestureRecognizerShouldBegin]; 
  106.  
  107.       } 
  108.  
  109.     } 
  110.  
  111.   } 
  112.  
  113.   
  114.  
  115.   return [super popViewControllerAnimated:animated]; 
  116.  
  117.  
  118.   
  119.  
  120. - (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated { 
  121.  
  122.   if (self.viewControllers.count == 1) { 
  123.  
  124.     self.enableRightGesture = YES; 
  125.  
  126.   } else { 
  127.  
  128.     UIViewController *destinationController = viewController; 
  129.  
  130.     if ([destinationController isKindOfClass:[HYBBaseViewController class]]) { 
  131.  
  132.       if ([destinationController respondsToSelector:@selector(gestureRecognizerShouldBegin)]) { 
  133.  
  134.         HYBBaseViewController *vc = (HYBBaseViewController *)destinationController; 
  135.  
  136.         self.enableRightGesture = [vc gestureRecognizerShouldBegin]; 
  137.  
  138.       } 
  139.  
  140.     } 
  141.  
  142.   } 
  143.  
  144.   
  145.  
  146.   return [super popToViewController:viewController animated:animated]; 
  147.  
  148.  
  149.   
  150.  
  151. @end  

这是通过重写所有的pop/push相关方法,通过判断是否要求支持右滑来设置。然后,我们要让某个控制器类在右滑返回或者点击返回之前,先调用我们的API判断,如下:

  1. #import "HYBBController.h" 
  2.  
  3.   
  4.  
  5. @implementation HYBBController 
  6.  
  7.   
  8.  
  9. - (void)viewDidLoad { 
  10.  
  11.   [super viewDidLoad]; 
  12.  
  13.   
  14.  
  15.   UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
  16.  
  17.   [button setTitle:@"返回" forState:UIControlStateNormal]; 
  18.  
  19.   [button addTarget:self action:@selector(onBack) forControlEvents:UIControlEventTouchUpInside]; 
  20.  
  21.   [button sizeToFit]; 
  22.  
  23.   [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 
  24.  
  25.   UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 
  26.  
  27.   self.navigationItem.leftBarButtonItem = btnItem; 
  28.  
  29.  
  30.   
  31.  
  32. - (BOOL)gestureRecognizerShouldBegin { 
  33.  
  34.   [self onBack]; 
  35.  
  36.   return NO
  37.  
  38.  
  39.   
  40.  
  41. - (void)onBack { 
  42.  
  43.   UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"标哥的技术博客" 
  44.  
  45.                                                       message:@"知道博客地址是什么吗?" 
  46.  
  47.                                                      delegate:self 
  48.  
  49.                                             cancelButtonTitle:@"不知道" 
  50.  
  51.                                             otherButtonTitles:@"知道", nil]; 
  52.  
  53.   [alertView show]; 
  54.  
  55.  
  56.   
  57.  
  58. #pragma mark - UIAlertViewDelegate 
  59.  
  60. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
  61.  
  62.   if (buttonIndex == 0) { 
  63.  
  64.   
  65.  
  66.   } else { 
  67.  
  68.     if ([self.navigationItem.title isEqualToString:@"VC6"]) { 
  69.  
  70.       NSUInteger index = self.navigationController.viewControllers.count - 3; 
  71.  
  72.       UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:index]; 
  73.  
  74.       [self.navigationController popToViewController:vc animated:YES]; 
  75.  
  76.     } else { 
  77.  
  78.       [self.navigationController popViewControllerAnimated:YES]; 
  79.  
  80.     } 
  81.  
  82.   } 
  83.  
  84.  
  85.   
  86.  
  87. @end  

这种方案确实实现了我们的需求。但是,有没有更简单的方案呢?今天可能是眼睛有点困的原因,在研究的时候没有意识到第四种方案。在我准备写这篇文章的时候,我再认识地理了一遍逻辑,发现还有非常简单的一种方案可以实现我的需求。

方案四(可靠,***)

方案四:将UIGestureRecognizerDelegate放在公共导航类HYBNavigationController里遵守,并设置代理为导航类,但是,只遵守-gestureRecognizerShouldBegin:代理方法。

  1. @interface HYBNavigationController () 
  2.  
  3.   
  4.  
  5. @end 
  6.  
  7.   
  8.  
  9. @implementation HYBNavigationController 
  10.  
  11.   
  12.  
  13. - (void)viewDidLoad { 
  14.  
  15.   [super viewDidLoad]; 
  16.  
  17.   
  18.  
  19.   self.interactivePopGestureRecognizer.delegate = self; 
  20.  
  21.  
  22.   
  23.  
  24. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { 
  25.  
  26.   BOOL ok = YES; // 默认为支持右滑反回 
  27.  
  28.   if ([self.topViewController isKindOfClass:[HYBBaseViewController class]]) { 
  29.  
  30.     if ([self.topViewController respondsToSelector:@selector(gestureRecognizerShouldBegin)]) { 
  31.  
  32.       HYBBaseViewController *vc = (HYBBaseViewController *)self.topViewController; 
  33.  
  34.      ok = [vc gestureRecognizerShouldBegin]; 
  35.  
  36.     } 
  37.  
  38.   } 
  39.  
  40.   
  41.  
  42.   return ok; 
  43.  
  44.  
  45.   
  46.  
  47. @end  

使用方法与第三种方案一样,是不是非常地简化了?看来是元宵给我的礼物啊,突然想到这样的办法。以前一直没有研究过interactivePopGestureRecognizer属性,这个属性是iOS7以后才有的,因此在项目中一直不能直接使用leftBarButtonItem处理,除非那个界面不要右滑返回。

现在,一切都明了了,想要使用leftBarButtonItem在公共基类控制器中统一调用API来设置就非常简单了,右滑返回手势也可以正常使用~

还等什么,赶紧试试吧!

***

如果你所使用的项目也有这样的需求,不防试试吧!笔者提供了demo的,因此可以先下载demo来看看效果哦!经过多次测试,笔者认为这是可行的方案,大家若在使用中出现问题,还请反馈与笔者,我也想了解是什么情况,当然也要找解决方案,共同进步嘛。

责任编辑:庞桂玉 来源: iOS大全
相关推荐

2014-09-24 11:52:37

微信企业号开发

2009-08-17 21:52:18

2021-06-04 17:30:50

iOS导航栏方案

2015-02-11 15:06:41

微信SDK

2010-04-23 00:03:18

全局负载均衡

2023-07-06 12:54:15

开源KelemetryKubernetes

2021-11-17 08:16:03

内存控制Go

2013-07-25 15:15:26

iOS开发学习iOS全局变量

2021-06-16 10:21:19

华为智慧

2021-06-21 10:52:49

华为AI

2010-12-21 17:38:12

2013-10-16 15:36:53

iOS优化

2013-10-14 18:37:57

2013-09-25 14:47:15

远程控制SDK

2011-10-21 15:59:51

深信服负载均衡

2012-08-08 15:34:18

负载均衡深信服

2013-04-27 10:07:51

飞利浦

2021-04-20 14:57:20

架构运维技术

2015-07-20 15:14:19

侧滑菜单功能多样

2021-10-11 19:34:03

全局格式项目
点赞
收藏

51CTO技术栈公众号