1

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

2
  • otherNav.storyboard?.instantiateInitialViewController() is returning nil and then you are using myVC! on nil value Commented Dec 1, 2017 at 7:16
  • I was having the same issue - turns out I didn't have a link to a Controller - followed the steps here: stackoverflow.com/questions/26311000/… Commented Jul 16, 2018 at 12:49

1 Answer 1

3
let storyboard = UIStoryboard(name: <other storyboard>, bundle: nil) let myVC = storyboard.instantiateViewController(withIdentifier: <your navigation controler>) 
Sign up to request clarification or add additional context in comments.

2 Comments

is the <other storyboard> the name of my storyboard file? e.g. "Home.storyboard"
without extension for example: let storyboard = UIStoryboard(name: "Main", bundle: nil)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.