How To Add Custom Code To Navigation Controller Back Button – XCode iOS

To make navigation bar “Back” button to work according to your need. Use one of the techniques below –

1. Replace the “Back” button with a new button. The only problem with this technique is that the new button does not appear like the cornered back button, but it appears square. The positing and size remains the same but design of the button changes to square.

Add the following code to “viewDidLoad” method of your view controller file –

1
2
3
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = item;

Add the following function anywhere in the same file –

1
2
3
-(void)back {
// back button code
}

2. The following code is placed in the “viewWillDisappear” function of your view controller file –

1
2
3
if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound) {
// back button code
}

This code checks for the self view controller to be present in the memory or not. If not then it assumes the back button is pressed. And the code in curly brackets gets executed.

The benefit of this code is that it does nothing to the back button, so it appears exactly the same as it should be.

Both the techniques are easy to implement. Use the one which suits your needs.

Namit Gupta

iOS Developer @GetVokal. Passionate about Mobile Apps. Loves to Design. You can reach him on his social profiles below.

Comments 8

  1. Thanks for this solution. It was helpful to me. I’m still having one problem though. In using option 2 above, what I am seeing is that both the normal back button behavior AND my custom “back button code” are getting executed. In my case my back button code is calling popToRootViewController. But I see two transitions when running the app: first it goes back one screen (the default back button behavior) then it goes back to the root (my back button code). Any suggestions on how to NOT execute the default back button behavior – just totally replace it?

    1. Hi Steve, if you are using option 2, don’t include popToRootViewController inside the if statement. This should fix your problem. Let me know if you still face it.

      1. If I do the popToRootController outside the if, then it happens even in cases I don’t want it to. But regardless of that, what is happening is I first see the previous controller appear (just for a split second), then I see the root controller appear. I want to go straight to the root controller, without seeing the intermediate one.
        I’m guessing that the reason I see the previous controller briefly, is that the back button behavior is still getting executed, then my custom behavior (inside the viewWillDisappear) gets executed. Do you know a way around this (only executing what is in viewWillDisappear and NOT executing the built-in back button behavior)?

          1. OK. Option 1 is basically what we originally had. I was asked to fix it, because we didn’t like how the back button looks different in this case from the rest of the app.
            In our case, it is using the left-facing blue chevron. I haven’t been able to find an image that is exactly the same as Apple’s.
            I’m also looking at the solution proposed here:
            http://stackoverflow.com/questions/32589800/change-destination-of-navigation-back-button

            In any case, I really appreciate you taking the time to try and help. Thanks!

Leave a Reply