This question already has answers here:
How to present UIAlertController when not in a view controller?

(36个答案)


3年前关闭。




我试图在我的iOS应用程序中显示来自AppDelegate的UIAlertController
以下是警报和本方法。
UIAlertController *alert = [UIAlertController alertControllerWithTitle:cTitle message:cMessage preferredStyle:UIAlertControllerStyleAlert];

//Configure alert and actions

[self.window.rootViewController presentViewController:alert animated:TRUE completion:nil];

但是,当我尝试显示警报时,它没有出现,并且我在控制台中收到以下警报。
Warning: Attempt to present <UIAlertController: 0x145f5d60> on <UINavigationController: 0x146590f0> whose view is not in the window hierarchy!

是什么引起了错误,我该如何解决?

最佳答案

如果要从didFinishLaunchingWithOptions启动它,也可以使用此代码。希望这会有所帮助。

dispatch_async(dispatch_get_main_queue(), {
              let importantAlert: UIAlertController = UIAlertController(title: "Action Sheet", message: "Hello I was presented from appdelegate ;)", preferredStyle: .ActionSheet)
            self.window?.rootViewController?.presentViewController(importantAlert, animated: true, completion: nil)
        })

以及最新的:
DispatchQueue.main.async {
    let alert = UIAlertController(title: "Hello!", message: "Greetings from AppDelegate.", preferredStyle: .alert)
    self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}

关于ios - 来自AppDelegate的当前UIAlertController ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26952061/

10-16 11:01