vApp
vApp

Reputation: 269

Instantiating Initial View Controller Provides Nil

For my tab bar controller, I want a transition for 4/5 tabs. The fifth tab i was able to successfully perform a different transition, but trying to add it to the other view controllers caused a crash.

Action Storyboard: This storyboard contains my tab bar controller, and my camera view controller. The transition for this view controller is the only one that works. When i try to transition different tabs that are not in the tab bar controllers storyboard, it crashes

Storyboard that contains tab bar controller

This is the storyboard with the problem

For example, I want to provide a different transition for the view controller in this storyboard. However, i get the error: Unexpectedly found nil while unwrapping an optional value Home Storyboard

This is my code:

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if let restoreID = viewController.restorationIdentifier {
        if restoreID == "NavigationCamera" {
        // This is the transition that works
            if let nav = tabBarController.viewControllers![tabBarController.selectedIndex] as? UINavigationController {
                print("Nav is allowed")
                let newVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "CameraView")
                let transition = CATransition()
                transition.duration = 0.25
                transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
                transition.type = kCATransitionPush
                transition.subtype = kCATransitionFromTop
                nav.view.layer.add(transition, forKey: nil)
                nav.pushViewController(newVC!, animated: false)
                return false
            }
        } else {
            if let otherNav = tabBarController.viewControllers![tabBarController.selectedIndex] as? UINavigationController {
                print("Other nav is allowed")
                let vcID = restoreID + "View"
                print(vcID)
                let myVC = otherNav.storyboard?.instantiateInitialViewController()
                let otherTransition = CATransition()
                otherTransition.duration = 0.25
                otherTransition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
                otherTransition.type = kCATransitionPush
                otherTransition.subtype = kCATransitionFade
                otherNav.view.layer.add(otherTransition, forKey: nil)
                // This is where the error occurs and crashes
                otherNav.pushViewController(myVC!, animated: false)
                return false
            }
        }
    }
    return true
}

Once again the error i get is:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Upvotes: 1

Views: 2191

Answers (1)

Tomasz Czyżak
Tomasz Czyżak

Reputation: 1118

let storyboard = UIStoryboard(name: <other storyboard>, bundle: nil)
let myVC = storyboard.instantiateViewController(withIdentifier: <your navigation controler>)

Upvotes: 3

Related Questions