0

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

Xcoder
  • 1,433
  • 3
  • 17
  • 37
Viktor
  • 75
  • 4
  • 13

2 Answers2

2

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
}
Xcoder
  • 1,433
  • 3
  • 17
  • 37
  • I did also include that and I still get the same rezults. – Viktor Dec 05 '17 at 15:46
  • @Viktor So when you click the save button, the if statement still gets executed? – Xcoder Dec 05 '17 at 15:48
  • In theory it shouldn't unless your save button is returning to the previous screen. If that's the case you might need to create a flag to check for that inside this if – Pedro Cavaleiro Dec 05 '17 at 15:51
  • @PedroCavaleiro that's why I'm asking the OP. – Xcoder Dec 05 '17 at 15:53
  • Yes when you click on save button it does go into the previous view controller(same as back button). Pressing each of the buttons results in the view controller returning to the previous view controller – Viktor Dec 05 '17 at 16:16
  • because I already have an unwind action added on the save button that exists this view controller(it is written in previous view controller) I cannot add another one. – Viktor Dec 05 '17 at 18:02
  • Can I add multiple action to the save button ? – Viktor Dec 05 '17 at 18:08
  • Thank for the help i solved this with the prepare function that I had – Viktor Dec 05 '17 at 18:29
  • No problem. Glad I could help :) – Xcoder Dec 05 '17 at 20:53
0

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

Pedro Cavaleiro
  • 835
  • 7
  • 21