重写系统中的UINavigationController 返回按钮的事件
.扩展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 .扩展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 < .) {
[UIView animateWithDuration:. animations:^{
subview.alpha = .;
}];
}
}
} return NO;
} @end .测试使用 #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 不会执行 } 参考:http://blog.csdn.net/a330416020/article/details/41700821
重写系统中的UINavigationController 返回按钮的事件的更多相关文章
- 重写系统中的UINavigationController返回按钮的事件
1.扩展UIviewController UIViewController+BackButtonHandler.h #import <UIKit/UIKit.h> @protocol Ba ...
- Ionic2 下处理 Android 设备下返回按钮的事件
原文发表于我的技术博客 本文分享了 Ionic2 下处理 Android 设备下返回按钮的事件,供参考. 原文发表于我的技术博客 代码中我分享了如何捕捉 Ionic2 项目在 Android 设备下返 ...
- 如何修改 UINavigationController、UINavigationBar 中 navigationItem 左侧 “返回” 按钮的名称
如果我们从 title 为 “首页” 的页面 A 点击进入一个子页面 B,那么在页面 B 的左上角将显示一个名为 “<首页” 的按钮.假设这个页面 A 叫 “你是我天边最美的云彩”,那在页面 B ...
- iOS 自定义UINavigationController返回按钮
主要代码如下: //自定义导航栏返回按钮 self.navigationItem.leftBarButtonItem = ({ //导航栏返回背景视图 UIView *view = [[UIView ...
- 修改UINavigationController返回按钮颜色
系统默认颜色是蓝色的 视觉效果非常难看 在push进的ViewController中写 //修改UINavigationController的文字颜色 self.navigationControlle ...
- 移动端开发在iOS系统中 new Date() 返回 NaN 的问题
问题: 通过 new Date() 函数将后台返回的时间('2021-11-25')获取时间戳.在 chrome 浏览器的手机模拟器中没有出现问题,但在 iPhone 真机测试的时候,显示的结果不符合 ...
- UINavigationController 返回按钮去掉文字
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetric ...
- C#委托的用法 在C#中我想在一个方法中调用另一个按钮的事件,怎样来实现?
最开始我也不清楚,后来我是这样想了. 1.事件和委托不是一个概念,你如果是调用control的事件,可以直接在其对应的事件eventhandler上attach自己的事件方法就好了如:this.But ...
- 截获导航控制器系统返回按钮的点击pop及右滑pop事件
前几天看了@栾小布的一篇文章:Custom backBarButtonItem,在跟着做的时候我又顺便扩展了一些,写此文章的目的是为了总结一下自己所写的东西,方便以后翻看容易,同时也是自己入行iOS一 ...
随机推荐
- Matrix, Her, Transcendence
The Matrix showed us a traditional perspective of computation theory, which is a hierarchical world ...
- uva 10245 The Closest Pair Problem_枚举
题意:求任意两点之间的距离的最少一个距离 思路:枚举一下就可以了 #include <iostream> #include<cstdio> #include<cmath& ...
- HDOJ 4417 - Super Mario 线段树or树状数组离线处理..
题意: 同上 题解: 抓着这题作死的搞~~是因为今天练习赛的一道题.SPOJ KQUERY.直到我用最后一种树状数组通过了HDOJ这题后..交SPOJ的才没超时..看排名...时间能排到11名了..有 ...
- lucas模板
ll PowMod(ll a,ll b,ll MOD){ ll ret=; while(b){ ) ret=(ret*a)%MOD; a=(a*a)%MOD; b>>=; } return ...
- Iterator(迭代器模式)--(超市管理者)
这个Iterator就是收银台干的活. package patterns.actions.iterator; public interface IteratorList { boolean isEmp ...
- ORACLE AWR概述及生成AWR报告
1.Overview of the Automatic Workload Repository The Automatic Workload Repository (AWR) collects, pr ...
- 性能强悍的开源关系数据库PostgreSQL
性能强悍的开源关系数据库PostgreSQL
- 《JavaScript 闯关记》之初探
当学习一门新的编程语言的时候,应该边学边做,反复演练以加深理解.因此,你需要一个 JavaScript 解释器.幸运的是,每一个 Web 浏览器都包含一个 JavaScript 解释器. 可以通过在 ...
- 关于UIScrollView属性跟方法的总结
iOS中UIScollView的总结 在iOS开发中可以说UIScollView是所有滑动类视图的基础,包括UITableView,UIWebView,UICollectionView等等,UIScr ...
- java之集合类特性对比分析列表
类集合框架有很多文章都列出了继承关系图,但是我没有找到更清晰的特性对比图,我这里根据使用选择条件总结对比罗列一下它们之间的一些特点.