美文网首页
iOS ~ AppDelegate中弹出UIAlertContr

iOS ~ AppDelegate中弹出UIAlertContr

作者: 阳光下的叶子呵 | 来源:发表于2021-11-05 17:22 被阅读0次
Objective-C:
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"不同意无法使用本软件,请退出应用" message:nil preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *album = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                }];
                [alert addAction:album];
                
                // 这样写:
                UIWindow   *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
                alertWindow.rootViewController = [[UIViewController alloc] init];
                alertWindow.windowLevel = UIWindowLevelAlert + 1;
                [alertWindow makeKeyAndVisible];
                
                [alertWindow.rootViewController presentViewController:alert animated:YES completion:nil];
swift:
var topWindow: UIWindow? = UIWindow(frame: UIScreen.main.bounds)
topWindow?.rootViewController = UIViewController()
topWindow?.windowLevel = UIWindow.Level.alert + 1

let alert = UIAlertController(title: "APNS", message: "received Notification", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel) { _ in
    // continue your work

    // important to hide the window after work completed.
    // this also keeps a reference to the window until the action is invoked.
    topWindow?.isHidden = true // if you want to hide the topwindow then use this
    topWindow = nil // if you want to hide the topwindow then use this
 })

topWindow?.makeKeyAndVisible()
topWindow?.rootViewController?.present(alert, animated: true, completion: nil)

相关文章

网友评论

      本文标题:iOS ~ AppDelegate中弹出UIAlertContr

      本文链接:https://www.haomeiwen.com/subject/nimrzltx.html