实例化初始视图控制器提供Nil

时间:2017-12-01 07:03:20

标签: ios swift xcode storyboard

对于我的标签栏控制器,我想要一个4/5标签的转换。第五个选项卡我能够成功执行不同的转换,但尝试将其添加到其他视图控制器导致崩溃。

  

动作故事板:此故事板包含我的标签栏控制器和我的摄像机视图控制器。此视图控制器的转换是唯一有效的转换。当我尝试转换不在标签栏控制器故事板中的不同标签时,它会崩溃

     

Storyboard that contains tab bar controller

这是有问题的故事板

  

例如,我想为此故事板中的视图控制器提供不同的转换。但是,我得到错误:   在展开可选值时意外地发现了nil   Home Storyboard

这是我的代码:

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
}

我得到的错误再次是:

  

线程1:致命错误:在解包可选值时意外发现nil

1 个答案:

答案 0 :(得分:0)

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