7

When using 3D Touch Shortcuts from the home screen I am trying to segue to different view controller.

The application is embedded within a UITabBarController and each tab root controller is a UINavigationController.

Here is how I attempted handling the shortcuts to load the view controller for each shortcut.

private func handleShortcutItem(shortcutItem: UIApplicationShortcutItem) {

    if let rootViewController = window?.rootViewController, let shortcutItemType = ShortcutItemType(shortcutItem: shortcutItem) {
    let sb = UIStoryboard(name: "main", bundle: nil)

        let helloVC = sb.instantiateViewControllerWithIdentifier("HelloVC") as! HelloViewController
        let goodbyeVC = sb.instantiateViewControllerWithIdentifier("GoodbyeVC") as! GoodbyeViewController

        switch shortcutItemType {
        case .Hello:
            rootViewController.presentViewController(helloVC, animated: true, completion: nil)
            break
        case .Goodbye:
            rootViewController.presentViewController(goodbyeVC, animated: true, completion: nil)
            break
        }
    }
}

With this code the shortcuts only open the application to the initial view controller and not to the helloVC and goodbyeVC controllers which are in different tabs.

I am presuming this is because the ViewControllers I am trying to load are embedded within a UINavigationController as well as embedded within the UITabBarController.

How can I presentViewController which is embedded within the UITabBarController and UINavigationController ?

UPDATE

I am unsure if the following works as I have not got a iPhone 6S with me atm. But I have changed the code to the following, hopefully this will load the selected tab index when the 3D Touch Action is performed. From there it should post a notification to the view controller to perform a segue.

private func handleShortcutItem(shortcutItem: UIApplicationShortcutItem) {

    if let rootViewController = window?.rootViewController, let shortcutItemType = ShortcutItemType(shortcutItem: shortcutItem) {
      let tababarController = rootViewController as! UITabBarController

        switch shortcutItemType {
        case .Hello:
            tababarController.selectedIndex = 1     
            NSNotificationCenter.defaultCenter().postNotificationName("performsegueHello", object: nil)
            break
        case .Goodbye:
            tababarController.selectedIndex = 4
            NSNotificationCenter.defaultCenter().postNotificationName("performsegueGoodbye", object: nil)
            break
        }
    }
}
RileyDev
  • 3,110
  • 3
  • 18
  • 49
  • Have you checked that rootViewController.presentViewController(helloVC, animated: true, completion: nil) or rootViewController.presentViewController(goodbyeVC, animated: true, completion: nil) is getting called? – beyowulf Jan 28 '16 at 16:33
  • Have you looked into what is being referred to as the 'Co-ordinator' pattern? It's a useful way of encapsulating navigation within a mobile app. http://iamsim.me/the-coordinator-pattern/ – Infinity James Jun 09 '17 at 22:10

1 Answers1

2

Please try the following code. I think you are not reaching the navigation controller of tab bar.you can do this by : let insideNvc = tvc?.selectedViewController as? UINavigationController

Now, here is your navigation controller you can present or push anything on it.

 private func handleShortcutItem(shortcutItem: UIApplicationShortcutItem) {

 let nvc = self.window?.rootViewController as? UINavigationController
 let tvc = nvc?.topViewController as? TabBarController
 let insideNvc = tvc?.selectedViewController as? UINavigationController

if let shortcutItemType = ShortcutItemType(shortcutItem: shortcutItem) {
let sb = UIStoryboard(name: "main", bundle: nil)

    let helloVC = sb.instantiateViewControllerWithIdentifier("HelloVC") as! HelloViewController
    let goodbyeVC = sb.instantiateViewControllerWithIdentifier("GoodbyeVC") as! GoodbyeViewController

    switch shortcutItemType {
    case .Hello:
        insideNvc?.presentViewController(helloVC, animated: true, completion: nil)
        break
    case .Goodbye:
        insideNvc?.presentViewController(goodbyeVC, animated: true, completion: nil)
        break
    }
}

}