使用Swift在didReceiveRemoteNotification中呈现特定的视图控制器 [英] Present specific view controller in didReceiveRemoteNotification with Swift

查看:176
本文介绍了使用Swift在didReceiveRemoteNotification中呈现特定的视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击推送通知时,我希望将它们带到我的应用中的特定表视图控制器。此表视图控制器嵌入在导航控制器中,该控制器嵌入在选项卡栏控制器(我的根视图控制器)中。下面显示的图像显示了这一点。

When a user taps a push notification, I want them to be taken to a specific table view controller in my app. This table view controller is embedded in a navigation controller, which is embedded in a tab bar controller (my root view controller). The image shown below visualizes this.

根视图标签栏控制器的故事板ID为HomeVC,类名为HomeViewController,导航控制器的故事板ID为SettingsNavigationVC ,表视图控制器的故事板ID为SettingsTableVC,类名为SettingsNavigationVC。

The root view Tab Bar Controller has a storyboard ID of "HomeVC" and a class name of "HomeViewController", the Navigation Controller has a storyboard ID of "SettingsNavigationVC", and the Table View Controller has a storyboard ID of "SettingsTableVC" and a class name of "SettingsNavigationVC".

我有推送通知工作。通过工作,我的意思是我可以从设备发送消息并在另一台设备上接收它,但是当接收器点击通知时,我似乎无法打开除根视图控制器之外的任何其他视图控制器。根据我正在使用的推送通知指南,我正在使用以下代码在 didReceiveRemoteNotification 方法中:

I have push notifications working. By working, I mean I can send a message from a device and receive it on another device, but when the receiver taps the notification I can't seem to get any other view controller to open other than the root view controller. According to the push notification guide I'm using, I'm using the following code in the didReceiveRemoteNotification method:

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

    let rootVC = self.window!.rootViewController
    if PFUser.currentUser() != nil {
        let settingsTableVC = SettingsTableViewController()
        rootVC?.navigationController?.pushViewController(settingsTableVC, animated: false)
    }
}

我做错了什么,或者我必须做些什么来呈现正确的视图控制器?

What am I doing wrong, or what must I do to present the right view controller?

推荐答案

试试这个

let storyboard = UIStoryboard(name: "YourStoryboardName", bundle: nil)
let rootVC = storyboard.instantiateViewControllerWithIdentifier("HomeVC") as! UITabBarController
if PFUser.currentUser() != nil {
    rootVC.selectedIndex = 2 // Index of the tab bar item you want to present, as shown in question it seems is item 2
    self.window!.rootViewController = rootVC
}

这篇关于使用Swift在didReceiveRemoteNotification中呈现特定的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆