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

Prompting an UIAlertView to user before navigating back to the previous controller in a UINavigationController stack

我正在尝试在导航到上一个控制器之前提示 UIAlertView 并在用户决定留在同一个视图控制器上时阻止导航。使用 CCTBackButtonActionHelper,可以轻松生成 UIALertView,但我面临的唯一问题是它会将后退按钮的颜色更改为灰色,就像单击任何禁用的 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];
}
    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

我有一个简单的解决方案,在不使用任何第三方组件的情况下对我来说效果很好。

#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 以将您自己的导航项与选择器一起使用。

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

实施事件并create/show在那里创建您的 UIAlertView。

#pragma mark - Delegates -
#pragma mark UIAlertViewDelegate

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

最后,使用 UIAlertViewDelegate,检查您的用户的选择并做出相应的响应。

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

有一个非常简单的解决方案。

我宁愿让 Apple 解决这个问题,但你的问题(和我的)可以是 通过在 navigationBar:shouldPopItem:

中插入以下内容解决
        auto item = navigationBar.topItem;
        item.hidesBackButton = YES;
        item.hidesBackButton = NO;