Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to presentViewController embedded in UITabBarController and UINavigationBarController

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
        }
    }
}
like image 689
RileyDev Avatar asked Jan 28 '16 15:01

RileyDev


1 Answers

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
    }
}

}

like image 187
Monika mitruka Avatar answered Nov 20 '22 03:11

Monika mitruka