当前位置:   article > 正文

iOS navigation (pop)返回按钮点击提示框_.navigation.back 触发弹窗

.navigation.back 触发弹窗

想了一段时间,还是通过博客来记录自己开发学习的过程,也让自己坚持学习,坚持进步
想下载源码的猛戳这里
本文章主要讲的是,点击系统自带的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];
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

下面是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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

对了在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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

当然这里没有静止手势右滑返回上一界面,需要这功能的自行百度

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/503764
推荐阅读
相关标签
  

闽ICP备14008679号