1.扩展UIviewController
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

2.扩展UINavigationController ,也可以继承 使用

@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(subview.alpha < 1.) {
[UIView animateWithDuration:.25 animations:^{
subview.alpha = 1.;
}];
}
}
} return NO;
} @end

3.测试使用

#import "ViewController.h
#import "UIViewController+BackButtonHandler.h" @implementation ViewController -(void) viewDidLoad
{
[super viewDidLoad];
self.title = [NSString stringWithFormat:@"Screen-%d", self.navigationController.viewControllers.count];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain
target:selfaction:@selector(onNextBtn:)];
} -(void) onNextBtn:(id)sender
{
[self.navigationController pushViewController:[ViewController new] animated:YES];
} -(BOOL) navigationShouldPopOnBackButton ///在这个方法里写返回按钮的事件处理
{ //这里写要处理的代码
[self.navigationController popViewControllerAnimated:YES];
return YES;//返回NO 不会执行 }

重写系统中的UINavigationController返回按钮的事件的更多相关文章

  1. 重写系统中的UINavigationController 返回按钮的事件

    .扩展UIviewController UIViewController+BackButtonHandler.h #import <UIKit/UIKit.h> @protocol Bac ...

  2. Ionic2 下处理 Android 设备下返回按钮的事件

    原文发表于我的技术博客 本文分享了 Ionic2 下处理 Android 设备下返回按钮的事件,供参考. 原文发表于我的技术博客 代码中我分享了如何捕捉 Ionic2 项目在 Android 设备下返 ...

  3. 如何修改 UINavigationController、UINavigationBar 中 navigationItem 左侧 “返回” 按钮的名称

    如果我们从 title 为 “首页” 的页面 A 点击进入一个子页面 B,那么在页面 B 的左上角将显示一个名为 “<首页” 的按钮.假设这个页面 A 叫 “你是我天边最美的云彩”,那在页面 B ...

  4. iOS 自定义UINavigationController返回按钮

    主要代码如下: //自定义导航栏返回按钮 self.navigationItem.leftBarButtonItem = ({ //导航栏返回背景视图 UIView *view = [[UIView ...

  5. 修改UINavigationController返回按钮颜色

    系统默认颜色是蓝色的 视觉效果非常难看 在push进的ViewController中写 //修改UINavigationController的文字颜色 self.navigationControlle ...

  6. 移动端开发在iOS系统中 new Date() 返回 NaN 的问题

    问题: 通过 new Date() 函数将后台返回的时间('2021-11-25')获取时间戳.在 chrome 浏览器的手机模拟器中没有出现问题,但在 iPhone 真机测试的时候,显示的结果不符合 ...

  7. UINavigationController 返回按钮去掉文字

    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetric ...

  8. C#委托的用法 在C#中我想在一个方法中调用另一个按钮的事件,怎样来实现?

    最开始我也不清楚,后来我是这样想了. 1.事件和委托不是一个概念,你如果是调用control的事件,可以直接在其对应的事件eventhandler上attach自己的事件方法就好了如:this.But ...

  9. 截获导航控制器系统返回按钮的点击pop及右滑pop事件

    前几天看了@栾小布的一篇文章:Custom backBarButtonItem,在跟着做的时候我又顺便扩展了一些,写此文章的目的是为了总结一下自己所写的东西,方便以后翻看容易,同时也是自己入行iOS一 ...

随机推荐

  1. openssl 加密

    OpenSSL是一个强大的安全套接字层密码库,Apache使用它加密HTTPS,OpenSSH使用它加密SSH, 但是,你不应该只将其作为一个库来使用,它还是一个多用途的.跨平台的密码工具.

  2. (五)boost库之随机数random

    (五)boost库之随机数random boost库为我们提供了许多的日常随机数生成器: 1.uniform_smallint:在小整数域内的均匀分布 2.uniform_int:在整数域上的均匀分布 ...

  3. Opencv下图像对鼠标事件的响应

    直接上代码: //////////////////////////////////////////////////////////////////////// // // 该程序从文件中读入一幅图像, ...

  4. Java导出excel并下载功能

    我们使用的导出并下载功能是利用一个插件叫POI的插件提供的导出功能,很实用:首先先导入Jar包: Jar包下载地址:http://poi.apache.org/   官方文档地址:http://poi ...

  5. UEditor用法

    UEditor是百度的编辑器,界面美观,功能强大,用起来还不错. 详见该链接http://ueditor.baidu.com/ 一.下载 http://ueditor.baidu.com/downlo ...

  6. Windows下搭建Eclipse+Android4.0开发环境

    官方搭建步骤: http://developer.android.com/index.html 搭建好开发环境之前须要下载以下几个文件包: 一.安装Java执行环境JRE(没这个Eclipse执行不起 ...

  7. 【MFC学习笔记-作业8-蝴蝶飞~】【什么鬼作业】

    作业要求: 用定时器控制蝴蝶在窗口废物,如图所示... 这是什么鬼作业啊...蝴蝶还要我这个手残手画啊233333333          (ノಠ .ಠ)ノ彡┻━┻ 不过多亏之前几个鬼作业的福 收获颇 ...

  8. JavaScript之cookie

    JavaScript通过Cookie实现简单的用户登录状态的保存.Cookie可以跨越多个网页使用,但不能跨域名使用,也不同跨浏览器使用. 1.设置cookie function SetCookie( ...

  9. 卸载cloudera manager

    卸载Cloudera-Manager sudo /usr/share/cmf/uninstall-cloudera-manager.sh 一直选择Yes 就好 卸载完成后,它会问你是否要将databa ...

  10. hdu 5442 Favorite Donut 最大表示法+kmp

    题目链接 给你一个字符串, 然后把他想象成一个环. 从某一个地方断开,然后逆时针或顺时针, 都可以形成一个字符串, 求字典序最大的那种. 输出断开位置以及是顺时针还是逆时针. 如果两个一样, 输出位置 ...