从 AppDelegate 呈现 UIAlertController [英] Present UIAlertController from AppDelegate

查看:22
本文介绍了从 AppDelegate 呈现 UIAlertController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 iOS 应用程序中从 AppDelegate 呈现一个 UIAlertController.以下是警报和当前方法.

I'm trying to present a UIAlertController from the AppDelegate in my iOS app. Below is the alert and the present method.

UIAlertController *alert = [UIAlertController alertControllerWithTitle:cTitle message:cMessage preferredStyle:UIAlertControllerStyleAlert];

//Configure alert and actions

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

但是,当我尝试显示警报时,它没有出现,我在控制台中收到以下警报.

However, when I try to present the alert, it doesn't appear and I get the following alert in the console.

Warning: Attempt to present <UIAlertController: 0x145f5d60> on <UINavigationController: 0x146590f0> whose view is not in the window hierarchy!

导致错误的原因是什么,我该如何解决?

What is causing the error and how do I fix it?

推荐答案

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

You can use this code as well if you want to launch it from didFinishLaunchingWithOptions.Hope this helps.

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)
}

这篇关于从 AppDelegate 呈现 UIAlertController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆