Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return button pressed in UIAlertController

I'm trying to use a UIAlertController for multiple purposes. It has two buttons, cancel and OK. I would like to add it to a method and return the button press, so I could just check for the user's response and act on it.

Now, I can't return a value inside block. So, how would I go about this?

Thanks.

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Atenção!", "Atenção!") message:NSLocalizedString(@"Você não finalizou a sua série. Se sair desta tela, irá zerar o cronômetro.", "") preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *cancelar = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancelar", "Cancelar") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action)
                               {
                                   [alertController dismissViewControllerAnimated:YES completion:nil];
                                   // I would like to return this button press to the method calling this one. 
                               }];
    [alertController addAction:cancelar];

    UIAlertAction *ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", "OK") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
                               {
                                   [alertController dismissViewControllerAnimated:YES completion:nil];
                                   // I would like to return this button press to the method calling this one.
                               }];
    [alertController addAction:ok];

    [self presentViewController:alertController animated:YES completion:nil];

UPDATE: Real use

When user presses back button, it will call a method to check a condition. If the condition is met, the alert is shown and user needs to decide to leave screen or not. That's why it would be great to return the answer to the IBAction back Button.

Note: The whole idea is to have other methods, besides the back Button, also show the alert and get an response from user.

like image 253
Jorge Avatar asked Apr 15 '15 10:04

Jorge


1 Answers

You can make use of blocks.

Create a method like this

- (void)alertWithResponse:(void (^)(BOOL didCancel))response {

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Atenção!", "Atenção!") message:NSLocalizedString(@"Você não finalizou a sua série. Se sair desta tela, irá zerar o cronômetro.", "") preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *cancelar = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancelar", "Cancelar") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

                                   [alertController dismissViewControllerAnimated:YES completion:nil];
                                   response(YES);
                               }];
    [alertController addAction:cancelar];

    UIAlertAction *ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", "OK") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

                                   [alertController dismissViewControllerAnimated:YES completion:nil];
                                   response(NO);
                               }];
    [alertController addAction:ok];

    [self presentViewController:alertController animated:YES completion:nil];
}

Now in your back button, call this method like this

- (IBAction)backButtonCliccked:(id)sender {

    //your button logic...
    //.
    //.
    //.
    [self alertWithResponse:^(BOOL didCancel) {
        if(didCancel) {
            //alert returned Cancel
        } else {
            //alert returned OK
        }
    }];
}
like image 115
Burhanuddin Sunelwala Avatar answered Oct 18 '22 05:10

Burhanuddin Sunelwala