Setting action for back button in navigation controller Setting action for back button in navigation controller ios ios

Setting action for back button in navigation controller


Try putting this into the view controller where you want to detect the press:

-(void) viewWillDisappear:(BOOL)animated {    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {       // back button was pressed.  We know this is true because self is no longer       // in the navigation stack.      }    [super viewWillDisappear:animated];}


I've implemented UIViewController-BackButtonHandler extension. It does not need to subclass anything, just put it into your project and override navigationShouldPopOnBackButton method in UIViewController class:

-(BOOL) navigationShouldPopOnBackButton {    if(needsShowConfirmation) {        // Show confirmation alert        // ...        return NO; // Ignore 'Back' button this time    }    return YES; // Process 'Back' button click and Pop view controler}

Download sample app.


Unlike Amagrammer said, it's possible. You have to subclass your navigationController. I explained everything here (including example code).