1

I want to send a user to a specific ViewController in my app once a notification is clicked.

I now that I can do something like this:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("Home") as? HomeViewController
presentedVC?.presentViewController(destinationViewController!, animated: true, completion: nil)

But my app has a tab bar and looks like this

Tab bar

tab1: navigationController -> VC1

tab2: navigationController -> VC2 -> HomeVC

tab: navigationController -> VC3

Each tab has a navigationController as a infront of it.

So how can I send the user to HomeVC? I must first select tab 2 then the navigation controller then push the user tvice:

tab2: navigationController -> VC2 -> HomeVC

And the other problem, if there any way to tell if the user is already in HomeVC? I dont want to send the user to the same VC if his already there.

user2636197
  • 3,982
  • 9
  • 48
  • 69

3 Answers3

4

You must have access to your UITabbarController in you UIApplicationDelegate or wherever you're handling the notification tap.

let tabBar:UITabBarController = self.window?.rootViewController as! UITabBarController //or whatever your way of getting reference is

So first you'll get the reference to UINavigationController in your second tab like this:

let navInTab:UINavigationController = tabBar.viewControllers?[1] as! UINavigationController

Now push your home view at second tab's navigation controller:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("Home") as? HomeViewController
navInTab.pushViewController(destinationViewController!, animated: true)

And finally switch your tab to second to show the just pushed home controller

tabBar.selectedIndex = 1

Keep in mind, this answer assumes that your application has already set the tab bar as the root view controller of application window prior handling the notification tap.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • Dont I also need pushViewController() Since the HomeVC is two levels down from the navigation controller?: **tab2: navigationController -> VC2 -> HomeVC** – user2636197 Nov 09 '16 at 15:27
  • Oh, I overlooked that. Yes you indeed need to push view controller instead of `presentViewController()` I've edited my answer. – Adil Soomro Nov 09 '16 at 15:29
  • But do I need to push it twice since its two levels down? – user2636197 Nov 09 '16 at 15:39
  • yes, check the `topViewController` and `visibleViewController` property of `UINavigationController`. – Adil Soomro Nov 09 '16 at 16:24
  • I am trying ``if(navInTab.topViewController.isKindOfClass(HomeViewController) != nil) { print("is open") } else { print("not open") }`` But I get the error saying UIView has no member isKindOfClass – user2636197 Nov 09 '16 at 16:38
  • Try `navInTab.topViewController is HomeViewControll‌​er` as stated here. [Using isKindOfClass with Swift](http://stackoverflow.com/a/24220444/593709) – Adil Soomro Nov 09 '16 at 16:42
0

Try something like this:

if let tabBarController = window?.rootViewController as? UITabBarController {
  tabBarController.selectedIndex = 1 // in your case the second tab
}

The idea is to switch to get the tab bar instance and switch it to your desired tab (where you have your view controller).

The above code works in AppDelegate / you can easily call it anywhere by getting the tabBarController instance.

Eugen Dimboiu
  • 2,733
  • 2
  • 26
  • 33
0

You can check which tab is selected by user with the method var selectedIndex: Int. You can check which view controller is present like this self.navigationController?.presentingViewController?.presentedViewController. This will solve your problem.

Anni S
  • 1,996
  • 19
  • 28
  • I did not understand: ``self.navigationController?.presentingViewController?.presentedViewController`` can you give me an example on how I use that in order to check if I already are on HomeVC – user2636197 Nov 09 '16 at 16:15
  • presentedViewController is property of UIViewController: The view controller that is presented by this view controller, or one of its ancestors in the view controller hierarchy. – Anni S Nov 09 '16 at 19:42