I am using the storyboard
for my app which is using UINavigationController
. I would like to add a confirmation dialog to the default back button so
Actually, you can find the Back button view and add UITapGestureRecognizer to it.
If you look at this image:
Using this code:
@interface UIView (debug)
- (NSString *)recursiveDescription;
@end
@implementation newViewController
...
NSLog(@"%@", [self.navigationController.navigationBar recursiveDescription]);
You can realize how to find the View of the Back button. It is always the last one in subviews array of the navigationbar.
2012-05-11 14:56:32.572 backBtn[65281:f803] >
| >
| >
| | >
| | >
| >
| >
So I used:
UIView *backButton = [[navBar subviews] lastObject];
[backButton setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alertMsg)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[backButton addGestureRecognizer:tapGestureRecognizer];
Tapping on back button and voilà: