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. Unix/Linux环境C编程入门教程(27) 内存那些事儿

    calloc() free() getpagesize() malloc() mmap() munmap()函数介绍 calloc(配置内存空间) 相关函数 malloc,free,realloc,b ...

  2. IPicture总结

    1.利用IPicture接口加载.显示图片 IPicture接口管理一个图片对象和它的属性.图片对象提供对Bitmap Icon Metafile的语言不相关的抽象支持.图像对象的主要接口是IPict ...

  3. VS2010启动奔溃

    最近由于项目需要,装了git,需要用bitbucket管理源代码.没想到装了git得插件后 vs2010不能启动,卸载重新安装也行,修复也不行. 解决方案: 删除 目录C:\Users\用户名\App ...

  4. sql 数据库 庞大数据量 需要分表

    17:04:05问下 在什么情况下 审核分区啊 ~..大熊..o○ 17:06:53这个要看应用~..大熊..o○ 17:07:37比如数据量很大,查询多是按照时间段查询,就可以用时间段来做分区~.. ...

  5. 在PHP中处理表单之—Checkbox

    原文翻译自:http://www.html-form-guide.com/php-form/php-form-checkbox.html 单个checkbox  形如: <form action ...

  6. OpenSuSE查看指定软件包是否安装(OpenSuSE使用RPM作为默认的软件包维护管理工具)

    suse 是 zypper se xxxxx 是搜索软件包 (查看已经安装的 软件包是否已经安装)

  7. Makefile里调用Shell注意点

    http://www.linuxidc.com/Linux/2012-04/59093.htm 大家经常编写和使用Makefile, Makefile里面也经常用到shell, 但对其中一些需要注意的 ...

  8. UEditor用法

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

  9. android xml布局文件属性说明

    android xml布局文件属性说明 [摘]android xml布局文件属性说明 LinearLayout和RelativeLayout 共有属性:java代码中通过btn1关联次控件androi ...

  10. java SecurityManager

    ---- 众所周知,Java语言具有完善的安全框架,从编程语言,编译器.解释程序到Java虚拟机,都能确保Java系统不被无效的代码或敌对的编译器暗中破坏,基本上,它们保证了Java代码按预定的规则运 ...