1

I'm currently trying to add 3D touch to my app. I've already achieved that my app shows the UIApplicationShortcutItems, but now I'm having trouble assigning an action to them.

This is my current code:

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void)
{
   let homeVC = HomeVC()

   if shortcutItem.type == "com.appName.actionType1"
   {
       homeVC.tabBarController?.selectedIndex = 1
   }

   if shortcutItem.type == "com.appName.actionType2"
   {
       homeVC.tabBarController?.selectedIndex = 2
   }
}

It wouldn't be so hard to do this, but the problem I'm having is that my HomeVC isn't the rootViewController.

Any suggestions? I'd really appreciate it.

mugx
  • 9,869
  • 3
  • 43
  • 55

1 Answers1

0

The problem is that you're creating a new instance of HomeVC each time, instead of checking for something like

if let homeVC = window.rootViewController as? HomeVC { ... }
Guy Kogus
  • 7,251
  • 1
  • 27
  • 32
  • Thank you, but the problem is that the `HomeVC` isn't the `rootViewController` –  Jan 03 '18 at 17:05
  • Then keep a reference to it, or at least to the tab bar controller, somewhere. – Guy Kogus Jan 03 '18 at 17:10
  • Ok, but how would I do that. That's basically what I'm trying already –  Jan 03 '18 at 17:35
  • You can try keeping it as a property in the app delegate. – Guy Kogus Jan 04 '18 at 11:06
  • But how, sorry I'm pretty new to this stuff –  Jan 04 '18 at 11:12
  • In `AppDelegate.swift` add `var tabBarController: UITabBarController!` and in `application(_:didFinishLaunchingWithOptions:)` add the line `tabBarController = window!.rootViewController as! UITabBarController` – Guy Kogus Jan 04 '18 at 11:27
  • yeah, but it is not the `rootViewController` –  Jan 04 '18 at 11:32
  • Then it depends on your app. You're gonna have to find a place where you have access to it and save it. I can't help you without looking at your entire project. – Guy Kogus Jan 04 '18 at 16:26