How to imagine a ViewController built into UITabBarController and UINavigationBarController

When using the 3D Touch keyboard shortcuts on the main screen, I try to switch to another view controller.

The application is built into the UITabBarController , and each root tab controller is a UINavigationController .

This is how I tried to handle 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, shortcuts open an application only to the initial view controller, and not to the helloVC and goodbyeVC , which are on different tabs.

I assume this is because the ViewControllers that I am trying to load is built into the UINavigationController and also built into the UITabBarController .

How can I presentViewController embed in UITabBarController and UINavigationController ?

UPDATE

I'm not sure the following works, as I don't have an iPhone 6S with me. But I changed the code to the next, hopefully this will load the selected tab index when 3D Touch Action is executed. From there, he should post a notification to the view manager to complete the session.

 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 } } } 
+7
ios swift uitabbarcontroller uinavigationcontroller presentviewcontroller
source share
1 answer

Please try the following code. I think you are not getting to the navigation controller in the tab bar. You can do this: let insideNvc = tvc? .SelectedViewController how? UINavigationController

Now, here is your navigation controller, which you can imagine or click 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 } } 

}

+1
source share

All Articles