本文為投稿文章,作者:標哥
前言
今天遇到一個小需求:在點擊導航欄返回按鈕之前,要先調用某個 API 並彈出 UIAlertView,根據用戶的選擇決定是返回還是留在當前控制器。舉個簡單例子——點擊左上角返回按鈕時調用 API 提示,點「知道」則返回,點「不知道」則留在當前頁。
問題來了:系統自帶的右滑返回手勢在點擊返回按鈕時無法攔截,它是自動的;因此只能改用 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 放到公共基類 HYBBaseViewController 遵守,實現代理並默認返回 YES(支持右滑返回)。若要讓某個控制器不支持右滑或返回前先執行操作,重寫該代理方法即可。
只在一個控制器裡時這能實現,但一旦該控制器被釋放,代理對象就變成 nil——因為代理是屬於導航條對象的,並不屬於單個控制器。
方案三(可行,但複雜)
把 UIGestureRecognizerDelegate 放到公共導航類 HYBNavigationController 遵守,設代理為導航類,並重寫 push/pop 相關的所有方法:
#import "HYBNavigationController.h"
#import "HYBBaseViewController.h"
@interface HYBNavigationController ()
@property (nonatomic, assign) BOOL enableRightGesture;
@end
@implementation HYBNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
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 {
if (self.viewControllers.count == 1) {
self.enableRightGesture = YES;
} else {
UIViewController *destinationController = viewController;
if ([destinationController isKindOfClass:[HYBBaseViewController class]]) {
if ([destinationController respondsToSelector:@selector(gestureRecognizerShouldBegin)]) {
HYBBaseViewController *vc = (HYBBaseViewController *)destinationController;
self.enableRightGesture = [vc gestureRecognizerShouldBegin];
}
}
}
return [super popToViewController:viewController animated:animated];
}
@end這裡通過重寫所有 push/pop 方法,根據是否需要右滑來設置開關。然後讓某個控制器在右滑或點擊返回前先調用 API 判斷:
#import "HYBBController.h"
@implementation HYBBController
- (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;
}
- (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) {
} else {
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];
}
}
}
@end這種方案確實實現了需求。但有沒有更簡單的辦法呢?可能是當天眼睛有點困,研究時沒想到第四種方案。等到要寫這篇文章、重新梳理邏輯時,才發現還有一種非常簡單的實現方式。
方案四(可靠,最優)
把 UIGestureRecognizerDelegate 放到公共導航類 HYBNavigationController 遵守,設代理為導航類,但只實現 gestureRecognizerShouldBegin: 一個方法:
@interface HYBNavigationController ()
@end
@implementation HYBNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
self.interactivePopGestureRecognizer.delegate = self;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
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;
}
@end使用方法與方案三相同,卻大大簡化了。看來這是元宵給我的禮物,讓我突然想到了這個辦法。以前一直沒研究過 interactivePopGestureRecognizer 這個屬性——它是 iOS 7 之後才有的,所以項目裡一直不能靠 leftBarButtonItem 統一處理,除非那個界面乾脆不要右滑返回。
現在一切都明朗了:想在公共基類控制器中統一調用 API 來設置 leftBarButtonItem,同時又保留右滑返回手勢,變得非常簡單。
還等什麼,趕緊試試吧!
最後
如果你的項目也有這樣的需求,不妨試試。筆者提供了 demo,可以先下載看效果。經過多次測試,筆者認為這是可行的方案;使用中若遇到問題,也歡迎反饋,我也想了解具體情況並一起找解決方案,共同進步。
源代碼
請到 GITHUB 下載:CoderJackyHuang