在导航返回到UINavigationController堆栈中的前一个控制器之前,向用户提示UIAlertView

我试图在导航到前一个控制器之前提示一个UIAlertView,并在用户决定停留在同一个视图控制器上时阻止导航。 使用CCTBackButtonActionHelper ,UIALertView可以很容易地生成,但我唯一的问题是,它会改变后退button的颜色为灰色,就像单击任何禁用的UIBarButton控件。 但是,单击导航栏上的任何位置都会恢复其原始颜色。 那么我怎么能防止改变它的颜色?

这就是我现在正在做的事情。

在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]; } 

在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]; } 

我有一个简单的解决scheme,这对我来说是完美的,没有使用任何第三方组件。

 #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:)]; } 

设置您的viewController使用您自己的导航项与select器。

 #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]; } 

实现事件并在那里创build/显示你的UIAlertView。

 #pragma mark - Delegates - #pragma mark UIAlertViewDelegate - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex) [self.navigationController popViewControllerAnimated:YES]; } 

最后,用UIAlertViewDelegate,检查你的用户的select并作出相应的响应。

在这里当用户确认我们popViewControllerAnimated或我们什么都不做。

有一个非常简单的解决scheme。

我宁愿有苹果公司解决这个问题,但你的问题(和我)可以通过在navigationBar:shouldPopItem:插入以下内容来解决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