Swift2 Swift:在didReceiveLocalNotification方法中显示UIAlertController

Swift2 Swift:在didReceiveLocalNotification方法中显示UIAlertController,swift2,uialertview,uialertcontroller,localnotification,Swift2,Uialertview,Uialertcontroller,Localnotification,我想在应用程序运行时触发通知时显示警报,这是我用于本地通知的。在本教程中,它使用UIAlertView来显示警报,并且可以正常工作,下面是代码: func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { // Point for handling the local notification when the app is op

我想在应用程序运行时触发通知时显示警报,这是我用于本地通知的。在本教程中,它使用
UIAlertView
来显示警报,并且可以正常工作,下面是代码:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    // Point for handling the local notification when the app is open.
    // Showing reminder details in an alertview
    UIAlertView(title: notification.alertTitle, message: notification.alertBody, delegate: nil, cancelButtonTitle: "OK").show()
}
但它警告说iOS 9中不推荐使用UIAlertView,因此我使用了
UIAlertController
,但当我运行它时,它会发出警告:

Attempt to present <UIAlertController: 0x7c322a00> on <xxx> whose view is not in the window hierarchy!
如何在
didReceiveLocalNotification
方法中显示UIAlertController?我还尝试:

 let activeViewCont = application.windows[0].rootViewController?.presentedViewController
 activeViewCont!.presentViewController(alert, animated: true, completion: nil)
试试这个:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {

    var topController : UIViewController = (application.keyWindow?.rootViewController)!

    while ((topController.presentedViewController) != nil) {

        topController = topController.presentedViewController!
    }

    let alert = UIAlertController(title: "", message: notification.alertBody, preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: {(action: UIAlertAction!) in}))

    topController.presentViewController(alert, animated: true, completion: nil)

})
希望有帮助。

试试这个:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {

    var topController : UIViewController = (application.keyWindow?.rootViewController)!

    while ((topController.presentedViewController) != nil) {

        topController = topController.presentedViewController!
    }

    let alert = UIAlertController(title: "", message: notification.alertBody, preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: {(action: UIAlertAction!) in}))

    topController.presentViewController(alert, animated: true, completion: nil)

})

希望有帮助。

self.window.rootViewController
这是您的项目中的导航控制器吗?@Nishant没有,我的项目中没有使用导航控制器
self.window.rootViewController
返回我自己创建的闪屏,当应用程序第一次打开时下载数据。但是我需要获取当前显示视图控制器
self.window.rootViewController
这是您项目中的导航控制器吗?@Nishant否,我没有在我的项目中使用导航控制器
self.window.rootViewController
返回我自己创建的闪屏,当应用程序第一次打开时下载数据。但是我需要得到当前的显示视图控制器。我以前也试过这个,它也没有帮助。它给出了相同的错误:
警告:尝试显示不在窗口层次结构中的视图我想我需要找到当前的显示视图控制器,但我不知道如何找到,例如,当我收到警告时,应用程序没有显示初始屏幕,而是显示另一个视图控制器感谢您提供的链接中的解决方案解决了我的问题issue@kakajan:太好了。我已将代码修改为正确答案。感谢您的帮助,我接受并更新了您的答案;)我以前也试过,也没用。它给出了相同的错误:
警告:尝试显示不在窗口层次结构中的视图我想我需要找到当前的显示视图控制器,但我不知道如何找到,例如,当我收到警告时,应用程序没有显示初始屏幕,而是显示另一个视图控制器感谢您提供的链接中的解决方案解决了我的问题issue@kakajan:太好了。我已将代码修改为正确答案。感谢您的帮助,我接受并更新了您的答案;)