我无法转换initialViewController

时间:2019-05-27 18:55:46

标签: ios swift

启动应用后,我想实例化一个ViewController。所以我实例化了控制器,但是却收到一个奇怪的错误

  

无法将类型'erkeb.HomeVC'(0x100bd8738)的值强制转换为'erkeb.ContainerVC'(0x100bd8990)

有人可以告诉我如何解决此问题吗?

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    fileprivate var containerVC = ContainerVC()

    var MenuContainerVC: ContainerVC{
        return containerVC
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        FirebaseApp.configure()

        containerVC = ContainerVC()

        window = UIWindow(frame: UIScreen.main.bounds)

        let sb = UIStoryboard(name: "Main", bundle: nil)
        var intialViewController = sb.instantiateViewController(withIdentifier: "WelkomVC")

        let userDefaults = UserDefaults.standard

        if userDefaults.bool(forKey: "WelkomIntroIsOk"){
            intialViewController = sb.instantiateInitialViewController() as! ContainerVC
        }

        window?.rootViewController = intialViewController
        window?.makeKeyAndVisible()

        //normaal juist onder lijn 28 maar menu schuift niet open
//        window?.rootViewController = containerVC
//        window?.makeKeyAndVisible()

        return true
    }

1 个答案:

答案 0 :(得分:1)

您无需将sb.instantiateInitialViewController()强制转换为ContainerVC。如果UserDefaults的值为true,则以编程方式创建ContainerVC实例并在intialViewController中进行分配。 ContainerVC将被分配为根视图控制器。

if userDefaults.bool(forKey: "WelkomIntroIsOk") {
    intialViewController = ContainerVC()
}