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

跟風遠走 提交于 2019-12-24 04:28:10

问题


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

回答1:


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.




回答2:


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;



回答3:


    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


来源:https://stackoverflow.com/questions/28168049/prompting-an-uialertview-to-user-before-navigating-back-to-the-previous-controll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!