首页 > 解决方案 > 无法转换视图控制器的值错误

问题描述

我的应用程序一直运行良好,直到今天在成功构建后突然出现以下错误:

Could not cast value of type 'FirebaseApp.HomeViewController' (0x101ab6aa0) to 'FirebaseApp.MenuViewController' (0x101ab6d30).


 Could not cast value of type 'FirebaseApp.HomeViewController' (0x101ab6aa0) to 'FirebaseApp.MenuViewController' (0x101ab6d30).

我的应用程序委托中的行

让控制器 = storyboard.instantiateViewController(withIdentifier: "mainView") as!菜单视图控制器

然后在此 AppDelegate 类中以红色突出显示:

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    FirebaseApp.configure()
    // Override point for customization after application launch.

    let authListener = Auth.auth().addStateDidChangeListener { auth, user in
       let storyboard = UIStoryboard(name: "Main", bundle: nil)

        if user != nil {
            //
            let controller = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController


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

        } else {
            // main screen
            let controller = storyboard.instantiateViewController(withIdentifier: "mainView") as! MenuViewController
            self.window?.rootViewController = controller
            self.window?.makeKeyAndVisible()
        }
    }
    return true
}

我的故事板中有 mainView 标识:在此处输入图像描述

我有一个 HomeViewController

类 HomeViewController:UIViewController, UITableViewDelegate, UITableViewDataSource {

其中有我的大部分代码。

我不确定这里出了什么问题,但我怀疑这与我的故事板中的错误命名有关。

编辑:我通过将“mainView”更改为“MenuViewController”解决了这个问题

标签: iosswiftxcode

解决方案


storyboardforMenuViewController你需要设置mainViewStoryboard IDnot Restoration ID

如下图:

在此处输入图像描述


推荐阅读