思路及效果:
利用runtime實(shí)現(xiàn)方法交換(method_exchangeImplementations)和利用runtime 給分類動(dòng)態(tài)綁定屬性kOriginDelegate鉴扫。kOriginDelegate綁定原self.interactivePopGestureRecognizer.delegate代理來響應(yīng)代理方法,添加一個(gè)接口 -(BOOL)navigationShouldPopOnBackButton;供外部訪問澈缺, 這個(gè)接口返回YES正常跳轉(zhuǎn)坪创,返回NO即可攔截返回按鈕的響應(yīng)事件。
具體實(shí)現(xiàn)如下:
UIViewController+BackButtonHandler.h文件:
#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
UIViewController+BackButtonHandler.m文件:
#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
使用實(shí)例:
/**
* 協(xié)議中的方法姐赡,獲取返回按鈕的點(diǎn)擊事件
*/
- (BOOL)navigationShouldPopOnBackButton
{
if ([self.mark isEqualToString:@"綁定手機(jī)號(hào)"]) {
UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"" message:@"綁定尚未完成误堡,確認(rèn)退出?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
[view show];
return NO;
}else{
return YES;
}
}