你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 阻攔UIViewController的popViewController事情

阻攔UIViewController的popViewController事情

編輯:IOS開發綜合

這篇文章為您講述阻攔UIViewController的popViewController事情的相關引見,詳細代碼請看下文

完成阻攔UIViewControllerpop操作有兩種方式:

自定義完成前往按鈕,即設置UIBarButtonItem來完成自定義的前往操作。 創立UINavigatonControllerCategory,來定制navigationBar: shouldPopItem:的邏輯。

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;
}

運用:

UIViewController當中引入頭文件
#import "UIViewController+BackButtonHandler.h"
UIViewController中完成navigationShouldPopOnBackButton辦法。
- (BOOL)navigationShouldPopOnBackButton{
    [[[UIAlertView alloc] initWithTitle:@"提示" message:@"確定前往上一界面?"
                               delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil] show];
    //renturn no 阻攔pop事情
    return NO;
}

參考:

IOS阻攔導航欄前往按鈕事情的正確方式

UIViewController-BackButtonHandler 

通本學習您是不是更理解IOS開發了呢.感激關注本站

【阻攔UIViewController的popViewController事情】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved