I'm trying to prompt a UIAlertView before navigating to the previous controller and preventing navigation if the user decides to stay on the same View Controller. Using CCTBackButtonActionHelper, the UIALertView can be easily generated but the only problem I'm facing is that it changes the color of the back button to gray just like any disabled UIBarButton control when clicked. However, clicking anywhere on the navigation bar restores its original color. So how could i prevent changing its color?
This is how I'm doing it right now.
In CustomNavigationContoller.m
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
BOOL should = [[CCTBackButtonActionHelper sharedInstance] navigationController:self navigationBar:navigationBar shouldPopItem:item];
if (!should) {
return NO;
}
return [super navigationBar:navigationBar shouldPopItem:item];
}
In CustomViewController.m
#pragma mark - Back button
- (void)cct_navigationBar:(UINavigationBar *)navigationBar willPopItem:(UINavigationItem *)item {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"Are you sure you want to go back?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[alertView show];
}
#pragma mark - Alert view delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.cancelButtonIndex == buttonIndex) {
return;
}
[self.navigationController popViewControllerAnimated:YES];
}