开发者

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 sh开发者_如何学Pythonown.

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

Thanks in advance.


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.


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.


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.


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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜