在 AppDelegate 中弹出框失败的问题

问题

在AppDelegate 中弹出 UIAlertController 总是失败, 后台报错

2015-07-24 14:25:17.547 MeApp[25391:2390835] Warning: Attempt to present on whose view is not in the window hierarchy!

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {        
        let alert = UIAlertController(title: "Hey", message: "While 111, I received a local notification: ", preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(alert,animated: true, completion: nil)
        //这种也不行        
        //self.window!.rootViewController!.presentViewController(alert,animated: true, completion: nil)
}

解决

另起了一个 Project 测试, 使用普通的 ViewController 上面的实现是没有问题的, 后来意识到在我的 APP 里面rootView 是用了 NavigationController, 是不是和这个有关?
最后在这里找到了方法

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {        
        let alert = UIAlertController(title: "Hey", message: "While 111, I received a local notification: ", preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        let navigationController = application.windows[0].rootViewController as! UINavigationController
        let activeViewCont = navigationController.visibleViewController
        activeViewCont.presentViewController(alert,animated: true, completion: nil)
}
2015-07-24 15:0174