ios - 实例化初始 View Controller 提供 Nil

标签 ios swift xcode storyboard

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

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

这是有问题的 Storyboard

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

这是我的代码:

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
}

我再次得到的错误是:

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>)

关于ios - 实例化初始 View Controller 提供 Nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47588214/

相关文章:

c++ - 我成功地能够在 C++ 中运行 Hello World 程序,但我看不到和输出。它在哪里?

php - 从 iOS 模拟器向本地主机发送 Post 请求

ios - 在 iOS 中,如何通过点击 Alert 弹出窗口中的 INSIDE 来关闭 Alert Controller?

swift - 按 double 对 UITableView 进行排序

ios - 如何让 Swift 与 Webview 交互?

c# - Unity XCode iOS 在启动时崩溃

ios - cocoapods 的重复定义,即使它们不是

ios - 嵌入式 AVPlayerControllerView 添加 AVTouchIgnoringView 阻止默认播放器的界面

python - 如何将数组转换为模型输入?

ios - UITableViewCell 文本在 DispatchQueue 上缩小