cancel viewWillDisappear

Hi guys,


I have a navigationcontroller based UI. if the user presses the back button, how can I prevent the view from disappearing if certain conditions are not met? In other words revert the back button press. I'm trying to do this within viewWillDisappear.


  1. Is this the best way to do it?
  2. How can I do it?


Thanks


Imran

You could disable or hide the back button when conditions are not met.

Hi Claude


That won't help in the current scenario. The screen has a form, and if anything has changed and the user presses the back button, I need to prompt him to see if he wants to save those changes, and act appropriately. But the alert is not showing as the view is unloaded before the prompt can be displayed

Did you try to customize this button ?


navigationItem.backBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: nil, action: #selector(buttonAction(_:))

and create


     @objc func buttonAction(_ sender: UIButton!) {
          // If test fails, just return
          // Otherwise, dismiss the controller
}

yep i have, and unfortunately that doesn't seem to work

Really, that would be much easier to help you if you showed the complete code of the class.

And tell exactly what does not work, with expected result, actual result, possible log information…

cancel viewWillDisappear
 
 
Q