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];
}
I have a simple solution, that has worked perfectly for me without using any third-party component.
#pragma mark - Configuration -
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationItem.titleView = self.titleView;
self.navigationController.navigationBarHidden = NO;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"red_cross"] style:UIBarButtonItemStyleBordered
target:self action:@selector(confirmPopViewController:)];
}
Setup your viewController to use your own navigationItem with a selector.
#pragma mark - Events -
- (void)confirmPopViewController:(id)sender
{
[[[UIAlertView alloc] initWithTitle:@"My ViewController" message:@"Do you really want to close ?" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"Yes", nil] show];
}
Implement the event and create/show a your UIAlertView There.
#pragma mark - Delegates -
#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex)
[self.navigationController popViewControllerAnimated:YES];
}
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.
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:
auto item = navigationBar.topItem;
item.hidesBackButton = YES;
item.hidesBackButton = NO;
you can set condition in viewWillAppear :(BOOL)animated ,
you can also use method
-(void )navigationController :(UINavigationController *)navigationController willshowViewController:(UIViewController *)ViewController animated :(Bool)animated
for setting the condition while transitioning to other view controller .
-(void)viewDidLoad{
[_Back setBackgroundImage:[MyViewController imageFromColor:[UIColor redColor]] forState:UIControlStateHighlighted];
[_Back setBackgroundImage:[MyViewController imageFromColor:[UIColor blueColor]] forState:UIControlStateNormal];
// [_Back setTintColor:[UIColor brownColor]];
}
+ (UIImage *)imageFromColor:(UIColor *)color {
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Hi i am trying this ,and i am able to change the color of back button when i click on it .
Though when i downloaded CCTBackButtonAction and running on xcode 6.1 beta , everything is fine without problem