0

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.)

icekomo
  • 9,328
  • 7
  • 31
  • 59

1 Answers1

2

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()
Community
  • 1
  • 1
  • I tried to do you first option, but it doesn't seem to be working. Any suggestions? I updated my question above to show you what I am trying. – icekomo Feb 07 '17 at 00:30
  • @icekomo I am not a Swift programmer, however, The solution was in option2 for Swift. Your updated code seems to be totally messed up now. You need to add the **AddPersonViewController** as **rootViewController** to your UINavigationController. Also you can not "Push" the "addPersonVC", you need to **self.window?.rootViewController = navigationController** , check option2 link as I said for Swift example you will see the answer there m8. –  Feb 07 '17 at 00:34
  • @icekomo http://stackoverflow.com/questions/26753925/set-initial-viewcontroller-in-appdelegate-swift you can check here for another answer too for Swift. –  Feb 07 '17 at 00:38
  • Thanks for the help. I updated what I am using, it's giving me a fatal error now: unexpectedly found nil while unwrapping an Optional value. I'm not 100% that I am using my NavigationController from the storyboard ID correctly. I know you're not a Swift guy, so thanks for the help so far! – icekomo Feb 07 '17 at 00:46
  • @icekomo Check my updated code to get a hint, let me know if it works. Beware, I maybe have some parsing errors you can fix that in Xcode if so. –  Feb 07 '17 at 00:51
  • that seems to out it in a navController inside maybe another NavController. WIth your code, how does it know that it should use my NavController from the storyboard? – icekomo Feb 07 '17 at 01:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135005/discussion-between-sneak-and-icekomo). –  Feb 07 '17 at 01:02