Search code examples
iosxcodeswift33dtouch

Trying to Embed VC into NavigationVC from performActionFor shortcutItem via 3d touch on App icon


I have an app that is using 3d touch to jump to a certain VC in my app. The issue is that when the app is launched normally, all of my VC's are embedded into a Navigation View Controller. But since I am skipping the launch sequence and jumping right into a VC, the VC that I jump to isn't embedded into the Nav VC.

Here is what I am trying in the App Delegate

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

    guard TouchActions(rawValue: shortcutItem.type) != nil else {
        print("3d not working")
        completionHandler(false)
        return
    }

   print("3d touch workig")

    let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let addPersonVC : AddPersonViewController = mainStoryboard.instantiateViewController(withIdentifier: "AddPerson") as! AddPersonViewController
    // pass the stack to the addPersonVC
    addPersonVC.coreDataStack = coreDataStack


    let navController:UINavigationController = mainStoryboard.instantiateViewController(withIdentifier: "navController") as! UINavigationController
    navController.pushViewController(addPersonVC, animated: true)


   // self.window? = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = navController
    self.window?.makeKeyAndVisible()

    completionHandler(true)
}

This code works, but you when I try to leave this VC, the app just sits there unresponsive. I some how need to embedded addPersonVC into the main Nav VC that is set up in storyboard. (The embedded navigation controller was set up in storyboard, incase that matters.)


Solution

  • Option1: Add a Storyboard Identifier to your UINavigationController , and instantiate the UINavigationController instead.

    Option2: Delete the UINavigationController from storyboard. Just initialize a new UINavigationController by code and set the rootView programatically.

    I am not a Swift developer, but since you don't see the examples I wrote I did some quick pasting to help you understand the basics, try this code:

    let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let addPersonVC : AddPersonViewController = mainStoryboard.instantiateViewController(withIdentifier: "AddPerson") as! AddPersonViewController
        // pass the stack to the addPersonVC
        addPersonVC.coreDataStack = coreDataStack
    
    let navController:UINavigationController = mainStoryboard.instantiateViewController(withIdentifier: "navController") as! UINavigationController
        navController.viewControllers = [addPersonVC]
    
    
        self.window?.rootViewController?.present(navController, animated: true, completion: nil)
        self.window?.makeKeyAndVisible()