iOS8之后提示框都使用UIAlertController,在UIViewController中調用也比較簡單,但是如果我們在AppDelegate中使用UIAlertController每天直接調用,需要一點小技巧來解決這個問題:
1.初始化UIAlertController:
UIAlertController *alertController=[UIAlertController alertControllerWithTitle:title message:content preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sureAction=[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"簡書-FlyElephant");
}];
[alertController addAction:sureAction];
2.初始化UIWindow:
UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
alertWindow.rootViewController = [[UIViewController alloc] init];
alertWindow.windowLevel = UIWindowLevelAlert + 1;
[alertWindow makeKeyAndVisible];
[alertWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
屬于一個開發中的小知識,希望對有需要的人幫助~