Long Tran Long Tran - 1 month ago 17
iOS Question

Can't click on anything after presenting UIAlertController from AppDelegate

I present a

UIAlerController
from an extra window's root view controller in AppDelegate.

//AppDelegate.h
@property (strong, nonatomic) UIWindow *alertWindow;

//SomeViewController.m
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action = [UIAlertAction actionWithTitle:@"action" style:UIAlertActionStyleDefault
handler:^(UIAlertAction* a){[alert dismissViewControllerAnimated:true completion:nil];});
[alert addAction:action];

appDelegate.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
appDelegate.alertWindow.rootViewController = [[UIViewController alloc] init];
appDelegate.alertWindow.windowLevel = UIWindowLevelAlert + 1;
[appDelegate.alertWindow makeKeyAndVisible];
[appDelegate.alertWindow.rootViewController presentViewController:alert animated:true completion:nil];


When this
UIAlertController
dismisses, I can't interact with anything on the screen anymore.. Any idea? Thanks.

Answer

Don't try to dismiss the alert controller. It will be dismissed when the alert action's handler is called.

Only in the handler hide or destroy the alertWindow.

Here try this:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"Action" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    appDelegate.alertWindow.hidden = YES;
    appDelegate.alertWindow = nil;
}];


                         [alert addAction:alertAction];
                         appDelegate.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
                         appDelegate.alertWindow.rootViewController = [[UIViewController alloc] init];
                         appDelegate.alertWindow.windowLevel = UIWindowLevelAlert + 1;
[appDelegate.alertWindow makeKeyAndVisible];
[appDelegate.alertWindow.rootViewController presentViewController:alert animated:true completion:nil];