导航栏后退按钮的操作 [英] Action to Navigation bar back button

查看:123
本文介绍了导航栏后退按钮的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户单击后退"按钮时显示带有确认"的警报.这就是我要添加操作的方式.

I want to show an alert with Confirmation when user clicks on back button. This is how I'm trying to add action.

self.navigationItem.hidesBackButton = true
let newBackButton = UIBarButtonItem(title: "<", style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.save(sender:)))
self.navigationItem.leftBarButtonItem = newBackButton

这工作正常,但是我想要默认的后退按钮图像而不是自定义标题.该怎么做?

This is working fine but I want the default back button image in it and not the custom title. How to do that ?

我也尝试了以下代码:

self.navigationItem.backBarButtonItem?.action = #selector(ViewController.save(sender:))

...但是也没有执行该动作.

...but the action is not performed, too.

推荐答案

这可能会有所帮助.这不会覆盖后退动作,但是您可以执行其他任务.

This might help. This wont override back action, but you can do additional task.

客观c

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if(self.isMovingFromParentViewController)
    {
        //On click of back or swipe back
    }
    if(self.isBeingDismissed)
    {
        //Dismissed
    }
    NSLog(@"%d",self.isBeingDismissed);
    NSLog(@"%d",self.isMovingFromParentViewController);
}

迅速

override func viewWillDisappear(_ animated: Bool)
{
    super.viewWillDisappear(animated);
    if self.isMovingFromParentViewController
    {
        //On click of back or swipe back
    }
    if self.isBeingDismissed
    {
        //Dismissed
    }
}

这篇关于导航栏后退按钮的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆