实例化初始视图控制器提供 Nil [英] Instantiating Initial View Controller Provides Nil

查看:36
本文介绍了实例化初始视图控制器提供 Nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是有问题的故事板

例如,我想为这个故事板中的视图控制器提供不同的过渡.但是,我收到错误:解包可选值时意外发现 nil

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

这是我的代码:

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

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

推荐答案

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

这篇关于实例化初始视图控制器提供 Nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆