重写系统中的UINavigationController返回按钮的事件
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返回按钮的事件的更多相关文章
- 重写系统中的UINavigationController 返回按钮的事件
.扩展UIviewController UIViewController+BackButtonHandler.h #import <UIKit/UIKit.h> @protocol Bac ...
- 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一 ...
随机推荐
- C based
switch(ch){case '\n': lines += 1; /*fail thru*/case ' ':case '\t': words += 1;default: c ...
- jdbc详解(三)
开源数据库连接池 l 现在很多 WEB 服务器 ( Weblogic , WebSphere , Tomcat) 都提供了 DataSoruce 的实现,即连接池的实现.通常我们把 DataSourc ...
- linux下的时间及时区设置
一.时间设置及同步 1修改系统时间 #date -s 06/18/14 #date -s 14:20:50 2命令查看.设置硬件时间 #hwclock --show 或者clock --show ...
- javascript模式——Facade
Facade模式为许多代码提供一个方便的接口,不现实代码实现的复杂性,这样,使用者只需要关心他的使用接口就可以使用. 下面来看一段Facade模式的运用,绑定事件在浏览器之间是不一样的,利用Facad ...
- 初始Knockout
Kncokout是一个轻量级的ui类库,通过应用MVVN模式得JavaScript前端简单化. MVVN模式:http://www.cnblogs.com/xueduanyang/p/3601471. ...
- js(1)不太注意的一些小事件
确认选择 除了向用户提供信息,我们还希望从用户那里获得信息.这里就用到了confirm 消息对话框. 语法:confirm(str); 参数说明: str: 在消息对话框中要显示的文本,返回的是布尔值 ...
- 安卓模拟器创建和使用SD卡的方法
安卓模拟器创建和使用SD卡的方法: 创建一个SD卡镜像文件 打开cmd,进入Android SDK安装路径下的tools目录下,输入如下命令:mksdcard 1024M sdcard.img 该命令 ...
- NSRangeFromString(<#NSString * _Nonnull aString#>) 和rangeOfString
NSRangeFromString NSString *str1 = @"abcdef"; NSString *str2 = @"1-105"; NSStrin ...
- ZOJ 1530 - Find The Multiple
Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal repr ...
- C++ buffer缓冲区的秘密
在搞数据库和C++进行连接的时候,遇到一个问题,就是如果前面用到了fflush(stdin)即清空缓冲区,就OK,如果不清空缓冲区就不能把记录加入到Mysql的数据库中, 但是即便如此,这个问题目前还 ...