Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法强制转换类型为';UIViewController';至';name.mainVC';-信号信号机_Ios_Swift_Xcode - Fatal编程技术网

Ios 无法强制转换类型为';UIViewController';至';name.mainVC';-信号信号机

Ios 无法强制转换类型为';UIViewController';至';name.mainVC';-信号信号机,ios,swift,xcode,Ios,Swift,Xcode,我对编码和制作入职屏幕非常陌生。我在我的入职故事板中使用了3个屏幕,在我尝试切换故事板之前,它们工作得很好。它在队列中显示“信号SIGABRT” let mainVC = storyboard.instantiateViewController(identifier: "mainVC") as! mainVC 错误: 2020-02-25 13:19:17.128892+0100我希望你能理解 [Storyboard]接口生成器文件中的未知类mainVC。不能 将“UIViewControll

我对编码和制作入职屏幕非常陌生。我在我的入职故事板中使用了3个屏幕,在我尝试切换故事板之前,它们工作得很好。它在队列中显示“信号SIGABRT”

let mainVC = storyboard.instantiateViewController(identifier: "mainVC") as! mainVC
错误:

2020-02-25 13:19:17.128892+0100我希望你能理解 [Storyboard]接口生成器文件中的未知类mainVC。不能 将“UIViewController”(0x110a5b940)类型的值强制转换为 “ihopeyoureokay.mainVC”(0x1060cc918)。2020-02-25 13:19:17.129648+0100 ihopeyoureokay[89853:2434827]无法强制转换类型的值 “UIViewController”(0x110a5b940)到“iOpeYoureOkay.mainVC” (0x1060cc918)

我在此文件中的完整代码是:

class ThirdViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func Gotoactual(_ sender: Any) {
        let storyboard = UIStoryboard(name: "actual", bundle: nil)
        let mainVC = storyboard.instantiateViewController(identifier: "mainVC") as! mainVC
               self.present(mainVC, animated: true, completion: nil)
    }
}
我的sceneDelegate看起来像这样:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        let launchedBefore = UserDefaults.standard.bool(forKey: "hasLaunched")
        let launchStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let mainStoryboard = UIStoryboard(name: "actual", bundle: nil)

        var vc: UIViewController

        if launchedBefore {
            vc = mainStoryboard.instantiateInitialViewController()!
        } else {
            vc = launchStoryboard.instantiateViewController(identifier: "firststoryboard")
        }

        UserDefaults.standard.set(true, forKey: "hasLaunched")

        self.window?.rootViewController = vc
        self.window?.makeKeyAndVisible()

        guard let _ = (scene as? UIWindowScene) else { return }
    }
}
这里还有另一个可能有帮助的屏幕截图:


有人知道我可能做错了什么吗?

您需要检查您的检查器,选择您的viewcontroller并检查以下内容:

as!mainVC
必需您有一个名为
mainVC


此外,您不需要创建两个不同的故事板,您可以创建两个不同的控制器,并根据您的条件创建视图控制器。

将故事板id设置为
mainVC
,以运行代码

首先,如果不使用
SwiftUI
,则不需要使用
SceneDelegate.swift
。您应该执行以下操作:

  • 确保您有一个名为
    actual.storyboard
    的故事板
  • 在属性导航器中将
    MainViewController
    storyboard id设置为
    mainVC
  • 不要使用强制展开,使用
    guard let
    if let
    语句
下面是一个示例代码:

class MainViewController: UIViewController {
    ...
}

class ThirdViewController: UIViewController {

    ...

    @IBAction func goToActual(_ sender: Any) {
        let storyboard = UIStoryboard(name: "actual", bundle: Bundle(for: MainViewController.self))
        guard let mainVC = storyboard.instantiateViewController(identifier: "mainVC") as? MainViewController else {
            print("Failed to get MainViewController instance!")
            return
        }
        self.present(mainVC, animated: true, completion: nil)
    }
}

我已经将我的故事板id设置为mainVC,但将其更改为guard let会有所帮助!多谢各位