简体   繁体   English

检测是否按下导航栏中的默认后退按钮

[英]Detect if default back button in navigation bar is pressed

I know that there are answers to this like make custom button and then make action but I don't want to change the default back button with arrow and I also tried like this: 我知道有这样的答案,例如制作自定义按钮,然后执行操作,但是我不想用箭头更改默认的后退按钮,我也尝试这样:

override func viewWillDisappear(_ animated : Bool) {
    super.viewWillDisappear(animated)
    if self.isMovingFromParentViewController {
     print("something")
    }
}

Here the problem is that this function gets called even if I press on save button that I also have in that view controller. 这里的问题是,即使我按下该视图控制器中同样具有的保存按钮,也会调用此函数。 I need to detect only when the back button is pressed 我只需要在按下后退按钮时进行检测

In your viewWillDisappear method, add the isMovingFromParentViewController property: 在viewWillDisappear方法中,添加isMovingFromParentViewController属性:

if self.isMovingFromParentViewController {
    //do stuff, the back button was pressed
}

EDIT: As had pointed out, if you are merely dismissing the view controller when the save button is pressed, you need a Bool value to check if the save button was pressed or the default back button. 编辑:正如已经指出的那样,如果您仅在按下保存按钮时解雇了视图控制器,则需要一个Bool值来检查是否按下了保存按钮或默认的后退按钮。

At the top of your class, define a Boolean value: 在类的顶部,定义一个布尔值:

var saveButtonPressed = false

I'm assuming that you have an @IBAction method linked to your save button. 我假设您有一个@IBAction方法链接到您的保存按钮。 If you don't, then I suggest you add that connection. 如果您不这样做,那么我建议您添加该连接。

In that method, set saveButtonPressed equal to true. 在该方法中,将saveButtonPressed设置为true。

Then, in your viewWillDisappear method, add this to the if condition: 然后,在您的viewWillDisappear方法中,将其添加到if条件:

if (self.isMovingFromParentViewController && !saveButtonPressed) {
    //do stuff, the back button was pressed
}

If the answer provided by Xcoder does not do what you want check this one 如果Xcoder提供的答案不符合您的要求,请检查此一项

https://stackoverflow.com/a/37230710/1152683 https://stackoverflow.com/a/372​​30710/1152683

It consists in creating an custom button, but with the arrow. 它包括创建一个自定义按钮,但带有箭头。 Check the answer for the whole code 检查整个代码的答案

The usage is pretty simple 用法很简单

weak var weakSelf = self

// Assign back button with back arrow and text (exactly like default back button)
navigationItem.leftBarButtonItems = CustomBackButton.createWithText("YourBackButtonTitle", color: UIColor.yourColor(), target: weakSelf, action: #selector(YourViewController.tappedBackButton))

// Assign back button with back arrow and image
navigationItem.leftBarButtonItems = CustomBackButton.createWithImage(UIImage(named: "yourImageName")!, color: UIColor.yourColor(), target: weakSelf, action: #selector(YourViewController.tappedBackButton))

func tappedBackButton() {

    // Do your thing

}

EDIT: 编辑:

Keeping the original button 保持原始按钮

Knowing that, for you this isn't working 知道这一点,对您来说这是行不通的

override func viewWillDisappear(_ animated : Bool) {
    super.viewWillDisappear(animated)
    if self.isMovingFromParentViewController {
     print("something")
    }
}

I can only assume that when tapping the save button you're navigating to the previous screen there for you must add a flag inside that function and then add a condition to that function like so 我只能假设点击保存按钮时,您正在导航至上一个屏幕,因为您必须在该函数内添加一个标志,然后再向该函数添加条件,如下所示

override func viewWillDisappear(_ animated : Bool) {
    super.viewWillDisappear(animated)
    if self.isMovingFromParentViewController && !save {
       // back pressed
    }
}

being the save variable a Bool that becomes true when the save button is tapped 是保存变量,当点击保存按钮时,布尔值变为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM