方法一:給UIViewController添加分類(推薦使用县习,已測試)
#import <UIKit/UIKit.h>
@protocol BackButtonHandlerProtocol <NSObject>
@optional
// Override this method in UIViewController derived class to handle 'Back' button click
- (BOOL)navigationShouldPopOnBackButton;
@end
@interface UIViewController (BackButtonHandler) <BackButtonHandlerProtocol>
@end
#import "UIViewController+BackButtonHandler.h"
#import <objc/runtime.h>
@implementation UIViewController (BackButtonHandler)
@end
static NSString *const kOriginDelegate = @"kOriginDelegate";
@implementation UINavigationController (ShouldPopOnBackButton)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originSelector = @selector(viewDidLoad);
SEL swizzledSelector = @selector(new_viewDidLoad);
Method originMethod = class_getInstanceMethod(class, originSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod = class_addMethod(class,
originSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originMethod),
method_getTypeEncoding(originMethod));
} else {
method_exchangeImplementations(originMethod, swizzledMethod);
}
});
}
- (void)new_viewDidLoad
{
[self new_viewDidLoad];
objc_setAssociatedObject(self, [kOriginDelegate UTF8String], self.interactivePopGestureRecognizer.delegate, OBJC_ASSOCIATION_ASSIGN);
self.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
}
#pragma mark - 按鈕
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
if([self.viewControllers count] < [navigationBar.items count]) {
return YES;
}
BOOL shouldPop = YES;
UIViewController* vc = [self topViewController];
if([vc respondsToSelector:@selector(navigationShouldPopOnBackButton)]) {
shouldPop = [vc navigationShouldPopOnBackButton];
}
if(shouldPop) {
dispatch_async(dispatch_get_main_queue(), ^{
[self popViewControllerAnimated:YES];
});
} else {
for(UIView *subview in [navigationBar subviews]) {
if(0. < subview.alpha && subview.alpha < 1.) {
[UIView animateWithDuration:.25 animations:^{
subview.alpha = 1.;
}];
}
}
}
return NO;
}
#pragma mark - 手勢
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer == self.interactivePopGestureRecognizer) {
UIViewController *vc = [self topViewController];
if([vc respondsToSelector:@selector(navigationShouldPopOnBackButton)]) {
return [vc navigationShouldPopOnBackButton];
}
id<UIGestureRecognizerDelegate> originDelegate = objc_getAssociatedObject(self, [kOriginDelegate UTF8String]);
return [originDelegate gestureRecognizerShouldBegin:gestureRecognizer];
}
return YES;
}
@end
使用方法:
在需要攔截pop方法的類里面寫
#pragma mark- BackButtonHandlerProtocol
-(BOOL)navigationShouldPopOnBackButton{
NSLog(@"我攔截到了分類方法2");
//相關(guān)邏輯處理
return YES;
}
方法二:繼承UINavigationController添加協(xié)議(不推薦使用)
#import <UIKit/UIKit.h>
@protocol LPNavigationControllerDelegate <NSObject>
@optional
- (BOOL)controllerWillPopHandler;
@end
@interface LPNavigationController : UINavigationController
@end
#import "LPNavigationController.h"
@interface UINavigationController (UINavigationControllerNeedshouldPopItem)
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item;
@end
@implementation UINavigationController(UINavigationControllerNeedshouldPopItem)
@end
// 以上幾行就是使用Category使UINavigationController將其實現(xiàn)的navigationBar:shouldPopItem:暴露出來趾断,
// 讓我們定制的子類可以調(diào)用
@interface LPNavigationController ()<UINavigationBarDelegate>
@end
@implementation LPNavigationController
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
UIViewController *vc = self.topViewController;
if ([vc respondsToSelector:@selector(controllerWillPopHandler)])
{
if ([vc performSelector:@selector(controllerWillPopHandler)])
{
return [super navigationBar:navigationBar shouldPopItem:item];
}
else
{
return NO;
}
}
else
{
return [super navigationBar:navigationBar shouldPopItem:item];
}
}
@end
使用方法:
在需要攔截pop方法的類里面寫
添加代理 <LPNavigationControllerDelegate>
#pragma mark- LPNavigationControllerDelegate
-(BOOL)controllerWillPopHandler{
NSLog(@"我攔截到了方法1");
//相關(guān)邏輯處理
return YES;
}
參考文獻:
一:http://www.reibang.com/p/a502d363c998
二:http://www.reibang.com/p/6376149a2c4c