前言
今天有个小需求,在点击导航条上的返回按钮之前要调用某个api,并弹出uialertview来显示,根据用户的选项判断是否是返回还是继续留在当前控制器。举个简单的例子,当点击导航条上的左上角返回按钮时,就调用我们的api来提示是否知道,点击知道则返回,点击不知道则继续留在当前控制器。
那么问题来了,导航自带的右滑返回手势在点击系统的返回按钮时,不会没有办法处理,那是自动的,因此就要想办法改成leftbarbuttonitem了,但是使用了leftbarbuttonitem就没有了右滑返回手势。
鱼和熊掌不可兼得?笔者自有办法!
笔者尝试写个demo来验证有什么办法可以解决,尝试了以下四种:
只在当前controller遵守uigesturerecognizerdelegate并设置代理为self
将uigesturerecognizerdelegate放在公共基类控制器遵守并设置代理为self,然后子类重写代理方法
将uigesturerecognizerdelegate放在公共导航类hybnavigationcontroller里遵守,并设置代理为导航类,然后重写push/pop相关的所有方法
将uigesturerecognizerdelegate放在公共导航类hybnavigationcontroller里遵守,并设置代理为导航类,但是,只遵守-gesturerecognizershouldbegin:代理方法
方案一(不可行)
方案一:只在当前controller遵守uigesturerecognizerdelegate并设置代理为self
为什么不可行呢?当想不测试怎么知道呢?光想是很难考虑全面的。于是写了个小demo来测试。
我们在该controller里这样写:
- (void)viewdidload {
[super viewdidload];
uibutton *button = [uibutton buttonwithtype:uibuttontypecustom];
[button settitle:@"返回" forstate:uicontrolstatenormal];
[button addtarget:self action:@selector(onback) forcontrolevents:uicontroleventtouchupinside];
[button sizetofit];
[button settitlecolor:[uicolor bluecolor] forstate:uicontrolstatenormal];
uibarbuttonitem *btnitem = [[uibarbuttonitem alloc] initwithcustomview:button];
self.navigationitem.leftbarbuttonitem = btnitem;
// 关键行
self.navigationcontroller.interactivepopgesturerecognizer.delegate = self;
}
一旦设置了代理为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相关的所有方法。
如实现如何下:
// hybnavigationcontroller.m
// navrightpangesturedemo
//
// created by huangyibiao on 16/2/22.
// copyright © 2016年 huangyibiao. all rights reserved.
#import "hybnavigationcontroller.h"
#import "hybbaseviewcontroller.h"
@interface hybnavigationcontroller ()
@property (nonatomic, assign) bool enablerightgesture;
@end
@implementation hybnavigationcontroller
self.enablerightgesture = yes;
self.interactivepopgesturerecognizer.delegate = self;
}
- (bool)gesturerecognizershouldbegin:(uigesturerecognizer *)gesturerecognizer {
return self.enablerightgesture;
- (void)pushviewcontroller:(uiviewcontroller *)viewcontroller animated:(bool)animated {
if ([viewcontroller iskindofclass:[hybbaseviewcontroller class]]) {
if ([viewcontroller respondstoselector:@selector(gesturerecognizershouldbegin)]) {
hybbaseviewcontroller *vc = (hybbaseviewcontroller *)viewcontroller;
self.enablerightgesture = [vc gesturerecognizershouldbegin];
}
}
[super pushviewcontroller:viewcontroller animated:yes];
- (nsarray *)poptorootviewcontrolleranimated:(bool)animated {
self.enablerightgesture = yes;
return [super poptorootviewcontrolleranimated:animated];
- (uiviewcontroller *)popviewcontrolleranimated:(bool)animated {
if (self.viewcontrollers.count == 1) {
self.enablerightgesture = yes;
} else {
nsuinteger index = self.viewcontrollers.count - 2;
uiviewcontroller *destinationcontroller = [self.viewcontrollers objectatindex:index];
if ([destinationcontroller iskindofclass:[hybbaseviewcontroller class]]) {
if ([destinationcontroller respondstoselector:@selector(gesturerecognizershouldbegin)]) {
hybbaseviewcontroller *vc = (hybbaseviewcontroller *)destinationcontroller;
self.enablerightgesture = [vc gesturerecognizershouldbegin];
}
return [super popviewcontrolleranimated:animated];
- (nsarray *)poptoviewcontroller:(uiviewcontroller *)viewcontroller animated:(bool)animated {
uiviewcontroller *destinationcontroller = viewcontroller;
return [super poptoviewcontroller:viewcontroller animated:animated];
@end
这是通过重写所有的pop/push相关方法,通过判断是否要求支持右滑来设置。然后,我们要让某个控制器类在右滑返回或者点击返回之前,先调用我们的api判断,如下:
#import "hybbcontroller.h"
@implementation hybbcontroller
uibutton *button = [uibutton buttonwithtype:uibuttontypecustom];
- (bool)gesturerecognizershouldbegin {
[self onback];
return no;
- (void)onback {
uialertview *alertview = [[uialertview alloc] initwithtitle:@"标哥的技术博客"
message:@"知道博客地址是什么吗?"
delegate:self
cancelbuttontitle:@"不知道"
otherbuttontitles:@"知道", nil];
[alertview show];
#pragma mark - uialertviewdelegate
- (void)alertview:(uialertview *)alertview clickedbuttonatindex:(nsinteger)buttonindex {
if (buttonindex == 0) {
if ([self.navigationitem.title isequaltostring:@"vc6"]) {
nsuinteger index = self.navigationcontroller.viewcontrollers.count - 3;
uiviewcontroller *vc = [self.navigationcontroller.viewcontrollers objectatindex:index];
[self.navigationcontroller poptoviewcontroller:vc animated:yes];
} else {
[self.navigationcontroller popviewcontrolleranimated:yes];
这种方案确实实现了我们的需求。但是,有没有更简单的方案呢?今天可能是眼睛有点困的原因,在研究的时候没有意识到第四种方案。在我准备写这篇文章的时候,我再认识地理了一遍逻辑,发现还有非常简单的一种方案可以实现我的需求。
方案四(可靠,最优)
方案四:将uigesturerecognizerdelegate放在公共导航类hybnavigationcontroller里遵守,并设置代理为导航类,但是,只遵守-gesturerecognizershouldbegin:代理方法。
bool ok = yes; // 默认为支持右滑反回
if ([self.topviewcontroller iskindofclass:[hybbaseviewcontroller class]]) {
if ([self.topviewcontroller respondstoselector:@selector(gesturerecognizershouldbegin)]) {
hybbaseviewcontroller *vc = (hybbaseviewcontroller *)self.topviewcontroller;
ok = [vc gesturerecognizershouldbegin];
return ok;
使用方法与第三种方案一样,是不是非常地简化了?看来是元宵给我的礼物啊,突然想到这样的办法。以前一直没有研究过interactivepopgesturerecognizer属性,这个属性是ios7以后才有的,因此在项目中一直不能直接使用leftbarbuttonitem处理,除非那个界面不要右滑返回。
现在,一切都明了了,想要使用leftbarbuttonitem在公共基类控制器中统一调用api来设置就非常简单了,右滑返回手势也可以正常使用~
还等什么,赶紧试试吧!
最后
如果你所使用的项目也有这样的需求,不防试试吧!笔者提供了demo的,因此可以先下载demo来看看效果哦!经过多次测试,笔者认为这是可行的方案,大家若在使用中出现问题,还请反馈与笔者,我也想了解是什么情况,当然也要找解决方案,共同进步嘛。
本文作者:佚名
来源:51cto