如何导航到推送通知上的控制器

时间:2015-03-26 08:55:50

标签: ios swift push-notification pushviewcontroller

我希望在收到推送通知后导航到某个视图控制器。导航后,导航堆栈应该像用户手动进入视图一样工作。

故事板:http://www.xpos.nl/xpos/images/common/storyboard.png

在AppDelegate.swift中我已经有了:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

    println("didReceiveRemoteNotification")

    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("MessageViewController") as MessageViewController

    let navigationController = self.window?.destinationViewController;

    navigationController?.pushViewController(destinationViewController, animated: false, completion: nil)

}

但是我得到一个错误,即destinationViewController不是窗口的一部分,或者如果我更正(在stackoverflow上尝试其他答案),没有任何反应。

2 个答案:

答案 0 :(得分:7)

destinationViewController不是窗口的一部分,因为它尚未添加,只是初始化。基于navigationViewController是你的rootViewController的假设,推送到你的destinationViewController,如下所示:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

    println("didReceiveRemoteNotification")

    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("MessageViewController") as MessageViewController

    let navigationController = self.window?.rootViewController as! UINavigationController

    navigationController?.pushViewController(destinationViewController, animated: false, completion: nil)

}

此外:要从“Bestellen”推送到“MessageViewController”然后弹出到“Berichten”,您还需要在这两者之间推送所有其他viewControllers。没有内置的功能或算法来做到这一点。

答案 1 :(得分:2)

试试这个

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

    println("didReceiveRemoteNotification")

    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("MessageViewController") as MessageViewController

    self.window?.rootViewController?.presentViewController(destinationViewController, animated: True, completion:nil)

    }