Search code examples
iosobjective-cuinavigationcontrolleruinavigationitem

How to call some code if users press back button


In UINavigationController?

For example, say I want to make sure that the UINavigationController is not animating when user press the back button.


Solution

  • If you aren't intending to intercept the back button tap itself but instead the act of the current view controller disappearing, you can use:

    - (void)viewWillDisappear:(BOOL)animated {
        if (self.isMovingFromParentViewController) {
            // handle back button press
        }
    }
    

    If you are sure you want to do the back button, you can create your own custom UIBarButtonItem and set it to the current controller's leftBarButtonItem. Be sure to call [self.navigationController popViewControllerAnimated:YES] after you have finished doing your own logic.