简体   繁体   中英

Custom behavior when “back” button in nav bar pressed

I know that you can add a custom "back" button to a UINavigationBar, but doing so removes the existing button with the tapered left side, as described in What happens when back button is pressed in navigationBar

Is there any way to keep the existing behavior and look of the back button, but also be informed when it is pressed?

Basically, I want my app to play a sound whenever any button is touched. I can do it with UITabBarController via its delegate, but the delegate for UINavgationBarController has no such functionality.

The following are the possibilities for a View Controller to disappear.

  1. Clicking on a button (other than backBarButtonItem ).
  2. Clicking on the backBarButtonItem

Either case viewWillDisappear: method will be called. Keep a boolean flag . Lets name it isAnyButtonClicked . And set isAnyButtonClicked = YES whenever any button is clicked. And override the viewWillDisappear: method, and play the sound if no button is clicked(ie., isAnyButtonClicked == NO ). Probably your viewWillDisappear: would look like,

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];

    if (!isAnyButtonClicked) {

        // Play sound
        isAnyButtonClicked = NO;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM