赞
踩
想了一段时间,还是通过博客来记录自己开发学习的过程,也让自己坚持学习,坚持进步
想下载源码的猛戳这里
本文章主要讲的是,点击系统自带的navigation返回按钮,弹出AlertView,点击确认后,才返回上一个页面,如图所示
下面是第二个界面.m的代码,其中-(void)alertView是在.h中公开的方法,方便在NaviViewController这个类中调用
#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,废话不多说,如下代码
#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中也需要进行配置
@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;
}
当然这里没有静止手势右滑返回上一界面,需要这功能的自行百度
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。