Know if Back button was pressed

On a view controller shown as part of a UINavigationController's stack, how do I tell if the Back button was pressed? The previously suggested answer from ages ago, to check isBeingDismissed(), seems to always return a false value.

Say the original (parent) viewController is A and it creates and displays a child view controller B. When the 'back' button is pressed in B you will get a call to viewWillDisappear in B and a call to viewWillAppear in A. You may need to set a variable in A when it first creates and displays B to indicate that if viewWillAppear is subsequently called it was called because of a 'back' action not an original viewDidLoad-viewWillAppear action


That would only tell me the view went away, not that the Back button was pressed. An exit segue from something like a cancel button might have been run, for example.

Your example is easily detected within the class in the ibaction called by the cancel button.

If the parent cares about which UI widget was tapped in some other view controller, then that's a sign you're doing something wrong with your app architecture. Each view controller should be responsible for its own UI only. If the parent VC does need to change its state based on something that happened in the child, it should be decoupled, either via a separate part of your model (perhaps a singleton) that represents the app state, or via a delegate on the child (methods such as didCancel, didSave, whatever). The key is to design the interface so that the parent knows about the *meaning* of whatever the user did, not what specific UI action was taken.

I never said the parent needs to know. I want the view controller currently on the stack, whose back button was pressed, to know that the view went away because of that button. The Cancel button was just one example. The view might go away because a phone call came in, or any other number of possibilities.

All that tells me is the view will/did go away. It doesn't tell me why.

You will have to detect all possible ways that the viewControlller can call viewWillDisappear and handle them all. There are app caused reasons for the call and most of them can be detected either within the viewController itself or in the appDelegate. There are non-app reasons and they will call the appDelegate's applicationWillResignActive: and may cause a call to viewWillDisappear. You may want to use viewDidDisappear.


Alternatively, explore using -(void)dealloc{ } . It won't be called by many things (e.g. tab bars, telephone calls) but will be called by the back button.

Maybe you have to replace back button with custom left bar button item.

Great answer. Do you know how to duplicate the large back chevron symbol?

When A view goes back it is removed from the parent view controller and this call happens yourViewController.willMove(toParentViewController: nil)

so maybe you can override the call and override func willMove(toParentViewController: UIViewController) { if toParentViewController == nil { // take action } super.willMove(toParentViewController: toParentViewController) }

Know if Back button was pressed
 
 
Q