如何在推送通知时导航到视图控制器
How to navigate to view controller on push notification
我想在收到推送通知后导航到某个视图控制器。导航后,导航堆栈应该像用户手动进入视图一样工作。
故事板: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 不是 window 的一部分,或者如果我更正它(在 Whosebug 上尝试其他答案),什么也没有发生。
destinationViewController 不是 window 的一部分,因为它还没有被添加,只是被初始化了。基于 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",您还需要将所有其他 viewController 推送到这两者之间。没有内置函数或算法可以做到这一点。
试试这个
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)
}
我想在收到推送通知后导航到某个视图控制器。导航后,导航堆栈应该像用户手动进入视图一样工作。
故事板: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 不是 window 的一部分,或者如果我更正它(在 Whosebug 上尝试其他答案),什么也没有发生。
destinationViewController 不是 window 的一部分,因为它还没有被添加,只是被初始化了。基于 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",您还需要将所有其他 viewController 推送到这两者之间。没有内置函数或算法可以做到这一点。
试试这个
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)
}