Return button pressed in UIAlertController

I am trying to use UIAlertController for several purposes. It has two buttons, cancel and OK. I would like to add it to the method and click the button so that I can simply check the user's response and act on it.

Now I can not return the value inside the block . So how can I do 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 the user clicks the back button , he is called by the status check method. If the condition is met, a warning is displayed, and the user must decide whether to leave the screen or not. Therefore, it would be great to return an answer to IBAction back button .

Note. The whole idea is that other methods, in addition to the back button , also display a warning and receive a response from the user.

+5
source share
1 answer

You can use 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 on your back button call this method as follows

 - (IBAction)backButtonCliccked:(id)sender { //your button logic... //. //. //. [self alertWithResponse:^(BOOL didCancel) { if(didCancel) { //alert returned Cancel } else { //alert returned OK } }]; } 
+9
source

All Articles