Present specific view controller in didReceiveRemo

2019-04-16 06:02发布

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.

enter image description here

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".

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?

2条回答
在下西门庆
2楼-- · 2019-04-16 06:42

Try this

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
}
查看更多
Deceive 欺骗
3楼-- · 2019-04-16 06:51

In order to push to the specific view controller, you must replace below line:

Yours:

rootVC?.navigationController?.pushViewController(settingsTableVC, animated: false)

Mine:

rootVC?.visibleViewController.navigationController?.pushViewController(settingsTableVC, animated: true)

And for presenting any view controller, you must use this:

rootVC?.visibleViewController.navigationController?.presentViewController(settingsTableVC, animated: false, completion: nil)
查看更多
登录 后发表回答