Detect when the user taps on the left side button of a UINavigationController [duplicate]
Pos开发者_运维技巧sible 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?
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.
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.
精彩评论