zoukankan      html  css  js  c++  java
  • 拦截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;
    }
    

    参考:

  • 相关阅读:
    点击子窗体给父窗体上的对象赋值
    框架使用及规范参考
    像Google日历一样的日程管理
    TreeView 和 Menu 的用法
    甘特图-svg版 支持客户端事件
    js获取DropDownList的选择项
    GridView,Repeater分页控件:WebPager(开源)
    TextBox 禁止客户端输入 前台通过JS赋值 并在后台获取
    对象实体 参考标准
    以编程方式控制ScriptManager
  • 原文地址:https://www.cnblogs.com/coolwxb/p/6222620.html
Copyright © 2011-2022 走看看