swift自定义导航控制器返回按钮事件

最近在项目中碰到点击返回按钮时要先进行确认的需求,网上度了一下找到了相应的解决方案(代码下载地址:https://github.com/onegray/UIViewController-BackButtonHandler)。

不是自己写的代码,就不白话了,简单说一下用法:GitHub上下载地址后,将下载的代码导入到自己的项目中(我是直接拖进去的),代码是Objective-C的,所以需要在头文件中导入.h文件;然后在自己的控制器中重写navigationShouldPopOnBackButton方法即可。

我的控制器重写navigationShouldPopOnBackButton方法如下,供参考:

 override func navigationShouldPopOnBackButton() -> Bool {
        let alertView = LGAlertView(title: "", message: "退出支付将取消此次操作,点‘确认’继续退出,点‘取消’继续此次操作", style: .alert, buttonTitles: ["确定"], cancelButtonTitle: "取消", destructiveButtonTitle: nil, actionHandler: {
            (alertView, text, buttonIndex) in
            
            if buttonIndex == 0 {
                var viewController:UIViewController?
                for index in 0..<(self.navigationController?.viewControllers.count)! {
                    if self.navigationController?.viewControllers[index] is 自己的控制器类名 {
                        viewController = self.navigationController?.viewControllers[index]
                    }
                }
                self.navigationController?.popToViewController(viewController!, animated: true)
            }
            
        }, cancelHandler: nil, destructiveHandler: nil)
        
        alertView.show(animated: true, completionHandler: nil)
        
        return false
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值