如何捕獲popViewControllerAnimated方法

如果使用系統(tǒng)自帶的leftBarButtonItem來處理的話,就需要使用自定義圖片,這種方法使用起來并不友好,正好在簡書上看到了一位兄弟發(fā)出來的解決方案,這里把代碼貼一下,有助于大家直觀的理解,項(xiàng)目地址:

https://github.com/XinStar1/AZXTallyBook

整體的思路就是使用新建一個(gè)Category用來攔截popViewControllerAnimated的方法,話不多說,直接上代碼

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"

@implementation UIViewController (BackButtonHandler)

@end

@implementation UINavigationController (ShouldPopOnBackButton)

- (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 {
        // Workaround for iOS7.1. Thanks to @boliva - http://stackoverflow.com/posts/comments/34452906
        for(UIView *subview in [navigationBar subviews]) {
            if(0. < subview.alpha && subview.alpha < 1.) {
                [UIView animateWithDuration:.25 animations:^{
                    subview.alpha = 1.;
                }];
            }
        }
    }
    
    return NO;
}

@end

調(diào)用方式如下

-(BOOL) navigationShouldPopOnBackButton
{
    //其他的可執(zhí)行方法
    return YES;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • *面試心聲:其實(shí)這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,210評論 30 471
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,269評論 25 708
  • 前言 由于最近兩個(gè)多月,筆者正和小伙伴們忙于對公司新項(xiàng)目的開發(fā),筆者主要負(fù)責(zé)項(xiàng)目整體架構(gòu)的搭建以及功能模塊的分工。...
    CoderMikeHe閱讀 27,134評論 74 270
  • 新上映的電影還沒有看,聽說是這倆人招募了正義聯(lián)盟的成員??
    Hi_木閱讀 169評論 0 2
  • 學(xué)習(xí)的主動性不高。主要體現(xiàn)在一下幾個(gè)方面。 1.雖然知道輸出倒逼輸入,每天都寫東西,但有時(shí)不是每天都學(xué)習(xí)輸入,有時(shí)...
    孤狼龍浩閱讀 755評論 0 0