【发布时间】:2021-03-12 00:40:01
【问题描述】:
在question/answer 的上下文中,我已经尝试过,但结果是没有显示警报。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let alertController = UIAlertController(title: "Error", message: "ABc", preferredStyle: UIAlertController.Style.alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
let win = UIWindow(frame: UIScreen.main.bounds)
let vc = UIViewController()
vc.view.backgroundColor = .clear
win.rootViewController = vc
win.windowLevel = UIWindow.Level.alert + 1 // Swift 3-4: UIWindowLevelAlert + 1
win.makeKeyAndVisible()
vc.present(alertController, animated: true, completion: nil)
return true
}
根据答案尝试 2。
【问题讨论】:
-
我不明白为什么
UIWindow需要参与。 -
为什么要在 AppDelegate 中显示警报?!
-
我在应用程序加载之前运行了一些与启动前相关的东西,如果出现任何问题,我需要提供一条警报消息。有什么解决办法吗?
-
解决方案是在加载后在第一个视图控制器中呈现。
-
@cora code sn-p 请,我尝试了下面的答案,但
self.window开头为零