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];
}
There's a very simple solution.
I'd rather have Apple fix the problem, but your issue (and mine) can be solved by inserting the following in
navigationBar:shouldPopItem:
I have a simple solution, that has worked perfectly for me without using any third-party component.
Setup your viewController to use your own navigationItem with a selector.
Implement the event and create/show a your UIAlertView There.
Finally, with the UIAlertViewDelegate, check if the choice of your user and respond accordingly.
Here when the user confirm we
popViewControllerAnimated
or we do nothing.