0

I want to open a specific ViewController from the TabBarController whenever a local notification is fired and their custom action is performed. I have used following line of code:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    switch response.actionIdentifier {
    case "first":
        DispatchQueue.main.async(execute: {
            self.first()
        })
    case "second":
        DispatchQueue.main.async(execute: {
            self.second()
        })
    default:
        break
    }


    completionHandler()
}

So it's a first() function:

 func first() {


    let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
    let tabBarController = storyboard.instantiateViewController(withIdentifier: "Root") as! UITabBarController
    let navigationController = storyboard.instantiateViewController(withIdentifier: "First") as! UINavigationController

    tabBarController.present(navigationController, animated: true) { 

    }

    self.window = UIWindow.init(frame: UIScreen.main.bounds)
    self.window?.tintColor = UIColor(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0)
    self.window?.rootViewController = tabBarController
    self.window?.makeKeyAndVisible()

}

Second function: second()

func second() {


    let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
    let tabBarController = storyboard.instantiateViewController(withIdentifier: "Root") as! UITabBarController
    let navigationController = storyboard.instantiateViewController(withIdentifier: "Second") as! UINavigationController

    tabBarController.present(navigationController, animated: true) {

    }

    self.window = UIWindow.init(frame: UIScreen.main.bounds)
    self.window?.tintColor = UIColor(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0)
    self.window?.rootViewController = tabBarController
    self.window?.makeKeyAndVisible()

}

And it's works well, but I cannot open the second ViewController while the first one is presented and the second notification is fired: In the console: Warning attempt to present ViewController...

Mannopson
  • 2,634
  • 1
  • 16
  • 32

2 Answers2

2

Use this:

tabBarController.selectedIndex = 1

Or this:

tabBarController.selectedViewController = tabBarController.viewControllers![1]

Where 1 can be any of the viewcontrollers presented by your tabBarController

iGhost
  • 1,009
  • 11
  • 21
J Manuel
  • 3,010
  • 22
  • 39
  • It's seems to useless. It's returns only 1 – Mannopson Feb 15 '17 at 16:07
  • 1
    It doesn't have to return anything. If you set selectedIndex to any number, the tab bar will change its tab to the index selected. If you set selectedIndex = 0, you will be switch to the first tab. selectedIndex = 1, second tab. And so on – J Manuel Feb 15 '17 at 17:32
  • Thanks for your help. I have not any experience with the UITabBarController class. – Mannopson Feb 16 '17 at 02:23
1

I had run into a similar issue and changing the selectedIndex did not work for me. Depending on the requirements of your project you can instantiate your ViewController and add it as a Subview and move to that Subview. When youre done make sure to remove that Subview.

let replyView = self.storyboard?.instantiateViewControllerWithIdentifier("replyView")
    self.addChildViewController(replyView!)
    self.view.addSubview(replyView!.view)
    replyView!.didMoveToParentViewController(self)
Mannopson
  • 2,634
  • 1
  • 16
  • 32
  • 1
    Inside whichever action you desire to change the `ViewController` with. If its a button action then call the above code inside the buttons action. – Aleksa Jovanovic Feb 15 '17 at 21:48