Viktor
Viktor

Reputation: 75

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

Upvotes: 0

Views: 1612

Answers (2)

Xcoder
Xcoder

Reputation: 1453

In your viewWillDisappear method, add the isMovingFromParentViewController property:

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.

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. If you don't, then I suggest you add that connection.

In that method, set saveButtonPressed equal to true.

Then, in your viewWillDisappear method, add this to the if condition:

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

Upvotes: 2

Pedro Cavaleiro
Pedro Cavaleiro

Reputation: 932

If the answer provided by Xcoder does not do what you want check this one

https://stackoverflow.com/a/37230710/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

Upvotes: 0

Related Questions