3

Is there any way I can catch the event when I press the back button in my iPhone application? Right now I am using a button with image named Back on it and defining the action for that.

My client requires default back button pointing to previous screen so based on click event I should be able make it point to particular screen so that title of that screen is shown.

In short i need back button to show the title i required, is it possible?

Thanks in advance.

Narasimha Reddy
  • 754
  • 6
  • 23

4 Answers4

6

UINavigationBarDelegate is probably the closest you can get to detecting whether the button has been pressed if you handle the – navigationBar:didPopItem: message.

This will be called either when the back button is pressed or when your code pops the view off the navigation controller stack itself. If your code is popping views manually then it should be trivial to set up a flag to indicate when your code initiated a pop and therefore you can determine when the back button was tapped

In your UINavigationBarDelegate implementation create a boolean property such as poppedInCode which you set to true just before your code performs a pop and implement the delegate like:

- (void) navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item
{
    if (!self.poppedInCode) {
        // back button was tapped
    }

    // set to false ready for the next pop
    self.poppedInCode = FALSE;
}

This has the advantage over the currently accepted answer in that it doesn't require subclassing of components that Apple's documentation says that you shouldn't be subclassing. It also retains all the behaviour of the built-in back button without you having to rewrite it.

Dolbz
  • 2,078
  • 1
  • 16
  • 25
3

There are two ways to do this:

  1. Implement the back button yourself and call the UINavigationController's - (UIViewController *)popViewControllerAnimated:(BOOL)animated

  2. Subclass UINavigationController and implement - (UIViewController *)popViewControllerAnimated:(BOOL)animated to do your processing and pass on the call to super.

  3. As suggested in another answer UINavigationBarDelegate allows you to detect whether the button has been pressed if you handle the – navigationBar:didPopItem: message.

Rog
  • 17,070
  • 9
  • 50
  • 73
  • 1
    The documentation for UINavigationController discourages you from subclassing so I wouldn't recommend option 2 – Dolbz Jul 06 '11 at 12:34
  • Implement the back button yourself means **using a button with back image in it and defining the action for that button click event rite?** if thats the case i am doing it in my code in current scenario as mentioned. – Narasimha Reddy Jul 06 '11 at 13:03
2

You can do your code in viewWillDisappear: method, and put flag bBackClicked = true in viewWillAppear, and if you push any other controller from current view controller, put flag bBackClicked = false.

jigneshbrahmkhatri
  • 3,627
  • 2
  • 21
  • 33
1

This method is simpler than the others provided. One advantage to this method is that you don't need to unnecessarily complicate your code with delegate methods. This method is also easier to implement if you already have a delegate for the UINavigationController, since it can only have a single delegate reference at a time.

-(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];
}
FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82