Luis Delgado
Luis Delgado

Reputation: 3734

How to show view controller with TabBarController navigation from push notification

(reposting due to initial wrong title)

When users receive a push notification for our app (and they tap on it), the app should open up a details screen. From a navigation perspective, the app usually has this structure anytime the user opens it:

TabBarController -> Navigation Controller -> View Controller

Once a use open the push notification, I'd like to instantiate a UIViewController. However, this VC should be part of the tabbarcontroller, so that the user can navigate to other areas of the app as well. Right now I am able to display the VC itself, but I can't make it appear as part of the tabbarcontroller:

class private func instantiateJobDetailsViewController(job: JobModel) {
        if let currentViewController = UIApplication.sharedApplication().delegate?.window??.rootViewController {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let jobDetailViewController = storyboard.instantiateViewControllerWithIdentifier("JobDetailController") as! JobDetailController
            jobDetailViewController.job = job
            currentViewController.presentViewController(jobDetailViewController, animated: true, completion: nil)
        } else {
            ClientHelper.displayAlertAsync("Error", message: "Info for support: cannot present jobDetails view controller", controller: nil)
        }
    }

Can someone provide some guidance about how to instantiate such a VC and, at the same time, preserve the tab bar controller so the user can navigate within the app?

Upvotes: 3

Views: 6225

Answers (2)

Luis Delgado
Luis Delgado

Reputation: 3734

For reference, here is how I resolved this situation:

class private func instantiateJobDetailsViewController(job: JobModel) {
        if let tabBarController = UIApplication.sharedApplication().delegate?.window??.rootViewController as? UITabBarController {
            tabBarController.selectedIndex = 0
            let currentNavigationController = tabBarController.selectedViewController as! UINavigationController
            let currentViewController = currentNavigationController.topViewController!
            currentViewController.performSegueWithIdentifier("JobDetailSegue", sender: job)
        } else {
            ClientHelper.displayAlertAsync("Error", message: "Info for support: cannot present jobDetails view controller", controller: nil)
        }
    }

So it is basically:

  • Get the instance of the tab bar controller.
  • Make sure you are in a relevant screen on the tab bar controller by setting its selectedIndex property.
  • Get the instance of the navigation controller
  • Get the instance of the view controller being presented by the navigation controller.

In this way, I can open up a relevant screen from a push notification while a) showing the tab bar controller and b) preserving the navigation element to allow the user to navigate back from the screen.

Upvotes: 8

Nguyen Hoan
Nguyen Hoan

Reputation: 1693

You try change to structure Navigation Controller -> TabBarController -> View Controller
You have a main navigationcontroller, set it is a global variable-a singleton ( var mainNavigation:UINavigationController?)
and when users receive a push notification, you can push to detailScreen from mainNavigation. Like this storyboardenter image description here

Upvotes: 0

Related Questions