在Swift中的AppDelegate中显示警报

Ork*_*ade 24 ios swift uialertcontroller

我尝试下一个代码片段:

var alert = UIAlertController(title: "Alert", message: "Cannot connect to : \(error!.localizedDescription)", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

在我的AppDelegate中,但它在控制台中打印出下一个错误:

Warning: Attempt to present <UIAlertController: 0x7ff6cd827a30> on <Messenger.WelcomeController: 0x7ff6cb51c940> whose view is not in the window hierarchy!
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个错误?

Jor*_*dez 29

这就是我现在用来做的事情.

var alertController = UIAlertController(title: "Title", message: "Any message", preferredStyle: .ActionSheet)
var okAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default) {
                    UIAlertAction in
                    NSLog("OK Pressed")
                }
var cancelAction = UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel) {
                    UIAlertAction in
                    NSLog("Cancel Pressed")
                }
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)


Ahm*_*adi 15

SWIFT 3

let alert = UIAlertController(title: "Test", message:"Message", preferredStyle: UIAlertControllerStyle.alert)

// add an action (button)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))

// show the alert
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

  • 虽然这个答案可以解决 OP 的问题,但值得详细说明这是如何实现解决方案的。简单地发布纯代码答案可能对 OP 或未来的用户没有帮助。请详细说明。 (2认同)

emr*_*raz 13

Swift 3.0或以上,在所有条件下工作,如标签栏的情况,如果有呈现的视图等.

let alert = UIAlertController(title: "Test", message:"Message", preferredStyle: UIAlertControllerStyle.alert)

// add an action (button)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))

// show alert
let alertWindow = UIWindow(frame: UIScreen.main.bounds)
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindowLevelAlert + 1;
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.present(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)


Chr*_*bst 13

根据Jorge的回答,更新了Swift 4

let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
        UIAlertAction in
        NSLog("OK Pressed")
    }
let cancelAction = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.cancel) {
        UIAlertAction in
        NSLog("Cancel Pressed")
    }
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)


Ras*_*iya 8

我有类似的问题.

我通过介绍UIAlertController来修复它Main Queue.

代码如下所示.

let alert = UIAlertController(title: "My Title", message: "My Message", preferredStyle: .alert)

let actionYes = UIAlertAction(title: "Yes", style: .default, handler: { action in
print("action yes handler")
})

let actionCancel = UIAlertAction(title: "Cancel", style: .destructive, handler: { action in
print("action cancel handler")
})

alert.addAction(actionYes)
alert.addAction(actionCancel)

DispatchQueue.main.async {
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)


Swi*_*y89 0

我建议不要在 AppDelegate 中执行此操作。应用程序委托旨在处理来自操作系统的委托功能,而不是实现警报视图之类的东西。

如果您想在此处呈现一个警报视图以在应用程序启动时显示,我将通过在您的第一个视图控制器中实现它来实现此目的。