Why is viewController.navigationController returning nil when called from performActionFor in SceneDelegate?

209 views Asked by At

I'm trying to create a quick action that will navigate to the 2nd view controller in my tab bar (vc1), then call a function which pushes another view controller (vc2) onto this one. Vc1 has a function called handleShowPopUp which contains the following code:

@objc func handleShowPopUp() {
    self.navigationController?.pushViewController(self.popUpWindow, animated: true)
}

This function works perfectly fine when it's called by one of the buttons on vc1. However, when I try to call it from my SceneDelegate in response to the quick action, it does nothing, and I've found out that when handleShowPopUp tries to push vc2, it returns nil. I tried explicitly setting a navigation controller to vc1 but even then it still returns nil. How can I fix this? Here is my SceneDelegate code:

    func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        completionHandler(handleShortcutItem(shortcutItem: shortcutItem))
    }
    
    func handleShortcutItem(shortcutItem: UIApplicationShortcutItem) -> Bool {
        var handle = false
        
        guard let shortcutType = shortcutItem.type as String? else {return false}
        
        switch(shortcutType){
        case "{identifier}":
            handle = true
            
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            guard let vc1 = storyboard.instantiateViewController(identifier: "vc1") as? VC else {return false}
            
            let tabVC = window?.rootViewController as! UITabBarController
            
            tabVC.selectedIndex = 1
            //move to vc1
            
            vc1.handleShowPopUp()
            //push vc2 (not working b/c navigationcontroller is nil)
            break
        default:
            break
        }
        
        return handle
    }
0

There are 0 answers