UIAlertAction completion block not called - iOS
Asked Answered
C

2

14

I have an iOS app with a UIAlertController in the form of a action sheet being presented in my app when the user taps on a button.

It all works great, apart from one thing, the completion blocks don't get called for some reason.

Here is my code:

// Setup the alert information/type.
UIAlertController *action_view = [UIAlertController alertControllerWithTitle:@"Test title" message:@"test message" preferredStyle:UIAlertControllerStyleActionSheet];

// Create and add the cancel button.
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {

    [action_view dismissViewControllerAnimated:YES completion:^{
        NSLog(@"asdhfgjk");
    }];
}];

// Add the action button to the alert.
[action_view addAction:cancel];

// Present the alert to the user.
[self presentViewController:action_view animated:YES completion:nil];

If you run that code you will see the line which dismisses the controller wont run and neither will the NSLog statement inside it. However, if you delete the NSLog and set the completion block to nil, then it does run.... why???

Thanks for your time, Dan.

Crutch answered 15/6, 2015 at 19:24 Comment(0)
L
44

Don't attempt to dismiss the alert controller. It will be dismissed for you by the time your alert action's handler is called.

Change the "cancel" action to:

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
    NSLog(@"asdhfgjk");
}];
Livvy answered 15/6, 2015 at 19:33 Comment(3)
Why the down vote? If there is something wrong with the answer, at least explain what the problem is.Livvy
I don't know who voted you down, but I'm voting your answer up and ticking it as it solved my problem perfectly, thanks. Come to think of it, I don't know why I was under the impression that I need to tell it to dismiss the alert controller anyway.Crutch
Upvoted because it helped me and because I've also been downvoted before without giving a reason. SO should require a reason to be given on a downvote.Meredi
A
3

The "cancel" action should not dismiss the view controller as mentioned by rmaddy. However, even if the "cancel" action is set to:

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
NSLog(@"asdhfgjk");}];

you may see the same issue with the completion block not being called. For example, implementing this (somewhat contrived) method:

- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {
   [[self presentedViewController] dismissViewControllerAnimated:flag completion:nil];
}

could also have this effect because the UIAlertController gets dismissed before the completion block is called.

Antisthenes answered 13/9, 2016 at 20:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.