dismissViewControllerAnimated does not work within a block

Daniel picture Daniel · Jan 21, 2015 · Viewed 11.5k times · Source

I try to close a UIViewController after an UIAlertController has been shown.

This is my code:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title 
                                                                         message:msg
                                                                  preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Accept" 
                                                   style:UIAlertActionStyleDefault 
                                                 handler:^(UIAlertAction *action)
             {
                 [self dismissViewControllerAnimated:YES completion:nil];
             }];

[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:^{}];

However, self never gets dismissed. Anyone knows how to solve this?

UPDATE

if I set [self dismissViewControllerAnimated:YES completion:nil]; outside of the block, it works.

Answer

diegomercado picture diegomercado · May 4, 2015

Just use [super.navigationController popViewControllerAnimated:YES];