Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Back navigation in iphone application

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.

like image 983
Narasimha Reddy Avatar asked Jan 30 '26 05:01

Narasimha Reddy


2 Answers

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.

like image 54
Dolbz Avatar answered Jan 31 '26 21:01

Dolbz


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.

like image 37
2 revsRoger Nolan Avatar answered Jan 31 '26 21:01

2 revsRoger Nolan