iOS修改导航返回按钮内容和返回指定页
2016-04-26 11:12
453 查看
方法非原创,来源资料查找和总结
1.修改导航返回按钮文字:
在push之前添加要返回按钮显示的内容:
UIBarButtonItem *returnButton = [[UIBarButtonItem alloc] init];
returnButton.title = @"返回";
self.navigationItem.backBarButtonItem = returnButton;
2.要控制返回按钮指定页,需要拿到点击返回按钮的点击事件,在此增加两个类别1.UIViewController+BackButtonHandler 2.UINavigationController+ShouldPopOnBackButton
在UIViewController+BackButtonHandler.h文件中:
@protocol BackButtonHandlerProtocol <NSObject>
@optional
-(BOOL)navigationShouldPopOnBackButton;
@end
#import <UIKit/UIKit.h>
@interface UIViewController (BackButtonHandler)<BackButtonHandlerProtocol>
@end
在UIViewController+BackButtonHandler.m中不用做处理
#import "UIViewController+BackButtonHandler.h"
@implementation UIViewController (BackButtonHandler)
@end
在UINavigationController+ShouldPopOnBackButton.m文件中
#import "UINavigationController+ShouldPopOnBackButton.h"
#import "UIViewController+BackButtonHandler.h"
@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 {
for(UIView *subview in [navigationBar subviews]) {
if(subview.alpha < 1.) {
[UIView animateWithDuration:.25 animations:^{
subview.alpha = 1.;
}];
}
}
}
return NO;
}
@end
至此只要在你需要控制返回按钮的页面添加一下方法就可以获取返回按钮的点击事件
-(BOOL) navigationShouldPopOnBackButton //在这个方法里写返回按钮的事件处理
{
//这里写要处理的代码****
return NO;//返回NO 不会执行
}
****
处理的代码可以根据需要自己编写,以下是控制返回指定页的代码
修改导航返回指定页
当push层级有多层时,要返回到其中某个页面:
确定需要返回的页面是层级中的第几个,然后根据指定页pop
UIViewController *Xview = self.navigationController.viewControllers[1];//取数组中第几个页面
[self.navigationController popToViewController:Xview animated:YES];
当要返回根视图时
[self.navigationController popToRootViewControllerAnimated:YES];
1.修改导航返回按钮文字:
在push之前添加要返回按钮显示的内容:
UIBarButtonItem *returnButton = [[UIBarButtonItem alloc] init];
returnButton.title = @"返回";
self.navigationItem.backBarButtonItem = returnButton;
2.要控制返回按钮指定页,需要拿到点击返回按钮的点击事件,在此增加两个类别1.UIViewController+BackButtonHandler 2.UINavigationController+ShouldPopOnBackButton
在UIViewController+BackButtonHandler.h文件中:
@protocol BackButtonHandlerProtocol <NSObject>
@optional
-(BOOL)navigationShouldPopOnBackButton;
@end
#import <UIKit/UIKit.h>
@interface UIViewController (BackButtonHandler)<BackButtonHandlerProtocol>
@end
在UIViewController+BackButtonHandler.m中不用做处理
#import "UIViewController+BackButtonHandler.h"
@implementation UIViewController (BackButtonHandler)
@end
在UINavigationController+ShouldPopOnBackButton.m文件中
#import "UINavigationController+ShouldPopOnBackButton.h"
#import "UIViewController+BackButtonHandler.h"
@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 {
for(UIView *subview in [navigationBar subviews]) {
if(subview.alpha < 1.) {
[UIView animateWithDuration:.25 animations:^{
subview.alpha = 1.;
}];
}
}
}
return NO;
}
@end
至此只要在你需要控制返回按钮的页面添加一下方法就可以获取返回按钮的点击事件
-(BOOL) navigationShouldPopOnBackButton //在这个方法里写返回按钮的事件处理
{
//这里写要处理的代码****
return NO;//返回NO 不会执行
}
****
处理的代码可以根据需要自己编写,以下是控制返回指定页的代码
修改导航返回指定页
当push层级有多层时,要返回到其中某个页面:
确定需要返回的页面是层级中的第几个,然后根据指定页pop
UIViewController *Xview = self.navigationController.viewControllers[1];//取数组中第几个页面
[self.navigationController popToViewController:Xview animated:YES];
当要返回根视图时
[self.navigationController popToRootViewControllerAnimated:YES];
相关文章推荐
- nagios联系人邮箱错误导致nagios无法启动
- iOS之谓词详解
- 我常去逛的iOS干货文章、blog等
- git 本地临时分支命名
- iOS可执行文件瘦身方法
- iOS最佳实践
- iOS性能优化
- iOS学习笔记73-iOS事件响应链中Hit-Test View的应用
- iOS开发的一些奇巧淫技
- 浅谈 iOS 版本号
- iOS开发多线程篇—单例模式(ARC)
- iOS中控制器的强引用释放问题
- iOS App集成Apple Pay教程(附示例代码)
- iOS 里面的常用数学函数
- IOS适配方法-标注篇
- ios开发中 线程、进程即多线程简单介绍
- iOS hot-patch框架 -- Rollout
- iOS中各种风格时间轴的设计及总结
- ios9横竖屏切换帖子
- iOS遍历程序内某个文件夹下所有文件的属性