views:

33

answers:

2

hi,

Is there a way to find out whether the back button (Navigation bar) is clicked for a particular view ? if yes how ?

A: 

First of all, I didn't try this myself. I don't know of a way to intercept the click on the button. What "might" work is the following: There is the UINavigationBarDelegate, which contains the navigationBar:shouldPopItem: event. This event is called before a navigationItem is removed from the stack of your navigationBar, so if you handle this event, you might be able to do whatever you want to archive.

Phlibbo
The OP doesn't state whether they're using a navigation controller or not. If not, Phlibbo's suggestion is a good one; however, if there's a nav controller involved then I don't think hooking into the nav bar delegate is a good idea since it will probably break the nav controller. In that case, a possible option would be to use the navigationController:willShowViewController:animated: method in UINavigationControllerDelegate.
Echelon
A: 

"handling events before" suggests you want to clean up or save state before the user leaves the view. In that case, I'd use viewWillDisappear: or viewDidDisappear:. If you need to distinguish between navigating back and navigating "forwards" (e.g. pushing a VC, or presenting a modal VC), you might be able to do something like:

-(void)viewDidDisappear:(BOOL)animated
{
  if (!self.parentViewController)
  {
     // back button pressed/modal VC dismissed/etc, hopefully
  }
}

I haven't tested this, but my understanding is that "Will"-methods are called before the VC hierarchy is changed, and "Did"-methods are called afterwards.

If you're saving state, you should also handle UIApplicationWillEnterBackgroundNotification.

tc.