How to create a confirmation Pop Up when pushing Back button in iOS?
Asked Answered
M

2

18

I want to add a pop up when someone pushes the "Back" button of my iOS App, to ask the user if he really wants to come back. Then, depending on the user's response, I would like to undo the action or proceed. I've tried to add the code in the viewWillDisappear function of my view and then write the proper delegate but it doesn't work, because it always change the view and then show the pop up. My code is:

    -(void) viewWillDisappear:(BOOL)animated {
       _animated = animated;
       if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
           UIAlertView *alert_undo = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
                                                                message:@"You could be    loosing information with this action. Do you want to proceed?"
                                                               delegate:self
                                                      cancelButtonTitle:@"Go back"
                                                      otherButtonTitles:@"Yes", nil];
           [alert_undo show];
       }
       else [super viewWillDisappear:animated];
   }

And the delegate implementation is:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Yes"])
    {
        [super viewWillDisappear:_animated];
    }
}

This is not working at all. Does anybody now a better way to do it or what could be wrong?

Thank you very much,

Mesomorphic answered 9/1, 2014 at 18:50 Comment(0)
M
15

Thanks for your answer, @staticVoidMan! I finally used your code with some modifications. The back button cannot be modified so one should create a additional button and hid the standard one. The only problem is the style of the new "Back" button, which is not equal than the standard one. The final code is:

- (void)viewDidLoad
{

    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *bbtnBack = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                 style:UIBarButtonItemStyleBordered
                                                                target:self
                                                                action:@selector(goBack:)];

    self.navigationItem.leftBarButtonItem = bbtnBack;

}

- (void)goBack:(UIBarButtonItem *)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                    message:@"...Do you want to proceed?"
                                                   delegate:self
                                          cancelButtonTitle:@"No"
                                          otherButtonTitles:@"Yes", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch(buttonIndex) {
        case 0: //"No" pressed
            //do something?
            break;
        case 1: //"Yes" pressed
            //here you pop the viewController
            [self.navigationController popViewControllerAnimated:YES];
            break;
    }
}
Mesomorphic answered 10/1, 2014 at 17:57 Comment(5)
@AlexSpencer Please avoid editing posts to improve the code with your own ideas. You can add a comment to suggest improvements. See the guidelines here: stackoverflow.com/help/editingLouielouis
@Louielouis it was a subtle change. but it's not my opinion. See this link:#4649923Abeokuta
@AlexSpencer That is talking about good programming practices, not about editing others content. In this site you should not edit posts to add new code for improvement. You can suggest that in a comment.Louielouis
@AlexSpencer Read this: meta.#266189Louielouis
@Louielouis alright, I will not edit code i will comment. That being said, here is my comment about the code above: ALWAYS have a default: break; within a switch case statementAbeokuta
M
19

Once -viewWillDisappear: is called, there's no stopping your viewController from disappearing.

You should ideally, override the navigationBar's back button and in it's method, display the alert (the rest being pretty much the same)

- (void)viewDidLoad
{
    //...
    UIBarButtonItem *bbtnBack = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                 style:UIBarButtonItemStyleBordered
                                                                target:self
                                                                action:@selector(goBack:)];

    [self.navigationItem setBackBarButtonItem: bbtnBack];
}

- (void)goBack:(UIBarButtonItem *)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                    message:@"...Do you want to proceed?"
                                                   delegate:self
                                          cancelButtonTitle:@"No"
                                          otherButtonTitles:@"Yes", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch(buttonIndex) {
        case 0: //"No" pressed
            //do something?
            break;
        case 1: //"Yes" pressed
            //here you pop the viewController
            [self.navigationController popViewControllerAnimated:YES];
            break;
    }
}

NOTE: Don't forget to declare <UIAlertViewDelegate> in the .h file of this viewController

Marv answered 9/1, 2014 at 18:57 Comment(1)
@Mesomorphic : you're welcome. mark your answer as accepted, i'll upvote it too :) anyways, technically... both [self.navigationItem setLeftBarButtonItem:bbtnBack]; or [self.navigationItem setBackBarButtonItem: bbtnBack]; should work (in your case)Marv
M
15

Thanks for your answer, @staticVoidMan! I finally used your code with some modifications. The back button cannot be modified so one should create a additional button and hid the standard one. The only problem is the style of the new "Back" button, which is not equal than the standard one. The final code is:

- (void)viewDidLoad
{

    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *bbtnBack = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                 style:UIBarButtonItemStyleBordered
                                                                target:self
                                                                action:@selector(goBack:)];

    self.navigationItem.leftBarButtonItem = bbtnBack;

}

- (void)goBack:(UIBarButtonItem *)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                    message:@"...Do you want to proceed?"
                                                   delegate:self
                                          cancelButtonTitle:@"No"
                                          otherButtonTitles:@"Yes", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch(buttonIndex) {
        case 0: //"No" pressed
            //do something?
            break;
        case 1: //"Yes" pressed
            //here you pop the viewController
            [self.navigationController popViewControllerAnimated:YES];
            break;
    }
}
Mesomorphic answered 10/1, 2014 at 17:57 Comment(5)
@AlexSpencer Please avoid editing posts to improve the code with your own ideas. You can add a comment to suggest improvements. See the guidelines here: stackoverflow.com/help/editingLouielouis
@Louielouis it was a subtle change. but it's not my opinion. See this link:#4649923Abeokuta
@AlexSpencer That is talking about good programming practices, not about editing others content. In this site you should not edit posts to add new code for improvement. You can suggest that in a comment.Louielouis
@AlexSpencer Read this: meta.#266189Louielouis
@Louielouis alright, I will not edit code i will comment. That being said, here is my comment about the code above: ALWAYS have a default: break; within a switch case statementAbeokuta

© 2022 - 2024 — McMap. All rights reserved.