0

Possible Duplicate:
Find out if user pressed the back button in uinavigationcontroller?

I want to play a sound when the user taps on the left-side button of a UINavigationController (the "back" button).

How can I detect this?

0

2 Answers 2

3

UINavigationController has a delegate that gets called every time a view controller is about to get pushed into view (and right after). You can make your view controller the delegate simply by calling this (e.g. in viewDidLoad):

[[self navigationController] setDelegate:self];

Then implement the method navigationController:willShowViewController:animated: in your view controller.

1

Another simple way is to implement your sound in the viewWillDisappear():

- (void)viewWillDisappear:(BOOL)animated {

//play my sound here

}

Or a more robust method, to ensure the sound only plays when the back button is pressed (in the case you have other ways the view may be popped from the navigation stack) is to subclass the UINavigationController and put in a custom action method for your back button. This page gives you an example of how to do this.

The example basically subclasses UINavigationController and over-rides the

- (UIViewController *)popViewControllerAnimated:(BOOL)animated 

method which is called when the back button is pressed.

Not the answer you're looking for? Browse other questions tagged or ask your own question.