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.
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
}
}];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With