iOS navigation (pop)返回按钮点击提示框
2016-01-09 20:29
295 查看
想了一段时间,还是通过博客来记录自己开发学习的过程,也让自己坚持学习,坚持进步
想下载源码的猛戳这里
本文章主要讲的是,点击系统自带的navigation返回按钮,弹出AlertView,点击确认后,才返回上一个页面,如图所示
下面是第二个界面.m的代码,其中-(void)alertView是在.h中公开的方法,方便在NaviViewController这个类中调用
下面是NaviViewController这个类,继承于UINavigationController,主要在这个类中判断navigation自带的back按钮的点击事件,这里是点击back后弹出alertView,废话不多说,如下代码
对了在appDelegate中也需要进行配置
当然这里没有静止手势右滑返回上一界面,需要这功能的自行百度
想下载源码的猛戳这里
本文章主要讲的是,点击系统自带的navigation返回按钮,弹出AlertView,点击确认后,才返回上一个页面,如图所示
下面是第二个界面.m的代码,其中-(void)alertView是在.h中公开的方法,方便在NaviViewController这个类中调用
[code]#import "ViewControllerB.h" @interface ViewControllerB () @end @implementation ViewControllerB - (void)viewDidLoad { [super viewDidLoad]; self.title = @"第二页"; } -(void)alertView { UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"返回" message:@"你愿意返回吗" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self.navigationController popViewControllerAnimated:YES]; }]; [alertVC addAction:cancelAction]; [alertVC addAction:sureAction]; [self presentViewController:alertVC animated:YES completion:nil]; }
下面是NaviViewController这个类,继承于UINavigationController,主要在这个类中判断navigation自带的back按钮的点击事件,这里是点击back后弹出alertView,废话不多说,如下代码
[code]#import "NaviViewController.h" #import "ViewControllerB.h" @interface NaviViewController ()<UIGestureRecognizerDelegate, UINavigationBarDelegate> //注意此类在appDelegate方法里面进行配置 @end @implementation NaviViewController -(BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item { UIViewController *vc = [self.viewControllers lastObject]; if ([vc isKindOfClass:[ViewControllerB class]]) { [(ViewControllerB *)vc alertView]; for (UIView *subView in [vc.navigationController.navigationBar subviews]) {//保证取消后左箭头颜色不变 if ([NSStringFromClass([subView class]) isEqualToString:@"_UINavigationBarBackIndicatorView"]) { [UIView animateWithDuration:0.25 animations:^{ subView.alpha = 1; }]; } } return NO; } return YES; } @end
对了在appDelegate中也需要进行配置
[code]@implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; _window.backgroundColor = [UIColor whiteColor]; [_window makeKeyAndVisible]; //注意NaviViewController是自己写得类 NaviViewController *navi = [[NaviViewController alloc]initWithRootViewController:[ViewController new]]; navi.navigationBar.backgroundColor = [UIColor blueColor]; _window.rootViewController = navi; return YES; }
当然这里没有静止手势右滑返回上一界面,需要这功能的自行百度
相关文章推荐
- iOS防止消息转发的crash
- IOS开发中常量的处理
- 基于IOS下的支付宝SDK的学习与使用——实现产品支付(一)
- IOS开发之深拷贝与浅拷贝(mutableCopy与Copy)详解
- iOS调用系统相册选取图片
- id和instancetype的区别
- [iOS]技术学习路线图(长期更新)[好久不见,
- [iOS]iOS9 3DTouch、ShortcutItem、Peek And Pop技术一览
- iOS开发 X-code技巧
- iOS framework 制作教程
- 详解iOS多线程
- ios多媒体
- Ios系统应用和系统服务
- iOS8 【xcode6中添加pch全局引用文件】
- iOS 进阶 第二十二天(0603)
- iOS 进阶 第二十一天(0531)
- iOS 进阶 第二十天(0520)
- iOS 进阶 第十九天(0423)
- iOS 进阶 第十八天(0423)
- iOS 进阶 第十七天(0420)