Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get notified of popped view in UINavigationController?

I want to perform a action when the user pressed the back button on my UINavigationController when arrived at a certain UIViewController.

Unfortunately it looks like UINavigationControllerDelegate doesn't have any methods to get notified of the popping of views.

As a workaround I now have in the viewDidDisappear method my action, that only gets fired when animated is YES. This works, but it's a bit ugly.

How should I do this properly?

like image 641
Peterdk Avatar asked Mar 09 '11 13:03

Peterdk


2 Answers

The most popular way of handling a pop from navigation view controller (as well as from modal) is implementing viewWillDisappear for your view controller.

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    if (self.isMovingFromParentViewController || self.isBeingDismissed) {
        // This view controller is being popped or dismissed
    }
}
like image 200
samwize Avatar answered Jan 26 '23 00:01

samwize


If you have a reference to the controller down the stack, the one which will show when this one is popped, you can register as a delegate and check it through

navigationController:willShowViewController:animated:
like image 32
Max_B Avatar answered Jan 26 '23 01:01

Max_B