【发布时间】:2015-01-06 21:01:08
【问题描述】:
我正在尝试显示来自 appDelegate 的 UIAlertView didReceiveRemoteNotification 当应用收到推送通知时。
我有这个错误:
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)}
【问题讨论】:
-
你是否试图让它从根视图控制器显示?
-
正如您在我的代码中看到的那样,我正在尝试使用 let activeVc = UIApplication.sharedApplication().keyWindow?.rootViewController 从活动 VC 中对它进行反播放
-
请注意,我的应用中有一个 tabBarController。
-
为什么你认为“rootViewController”必然是“活跃的VC”?
-
我在想.keyWindow?给出“活跃的VC”。如果没有,你怎么得到它?