如何从appDelegate呈现UIAlertView [英] How to present UIAlertView from appDelegate
本文介绍了如何从appDelegate呈现UIAlertView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我的应用收到推送通知时,我试图在
didReceiveRemoteNotification
的appDelegate中显示UIAlertView。
I m trying to display a UIAlertView from the appDelegate in didReceiveRemoteNotification when the app receive a push notification.
我有这个错误:
Warning: Attempt to present <UIAlertController: 0x14c5494c0> on <UINavigationController:
0x14c60ce00> whose view is not in the window hierarchy!
这是我的代码:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: NSDictionary) {
var contentPush: NSDictionary = userInfo.objectForKey("aps") as NSDictionary
var message = contentPush.objectForKey("alert") as String
let alertController = UIAlertController(title: "Default Style", message: message, preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ...
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
let photoPushedVc = self.storyboard.instantiateViewControllerWithIdentifier("CommentTableViewController") as CommentTableViewController
println("the fetched post is \(post)")
photoPushedVc.post = post
let activeVc = UIApplication.sharedApplication().keyWindow?.rootViewController
activeVc?.presentViewController(photoPushedVc, animated: true, completion: nil)
}
alertController.addAction(OKAction)
let activeVc = UIApplication.sharedApplication().keyWindow?.rootViewController
activeVc?.presentViewController(alertController, animated: true, completion: nil)}
推荐答案
好的,我终于明白了,你需要在尝试呈现你的alertController之前找到活动的VC:
Ok i finally got it, you need to find the active VC using this before trying to present your alertController :
let navigationController = application.windows[0].rootViewController as UINavigationController
let activeViewCont = navigationController.visibleViewController
activeViewCont.presentViewController(alertController, animated: true, completion: nil)
这篇关于如何从appDelegate呈现UIAlertView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文