在应用程序委托中识别和加载视图控制器时出现“使用未解决的标识符'rootViewController'错误”

时间:2019-04-13 16:57:55

标签: swift uiviewcontroller

阅读Emil Atanasov的通过构建应用程序学习Swift ,如果用户已经登录,我正在尝试在应用程序委托中编写代码以在TabbarViewController上启动应用程序(而是比SignInViewController)。但是,尽管为登录屏幕和主视图控制器编写了两个类似的功能(与本书相同),但在加载主视图控制器的功能上却遇到了很多错误-openMainViewController()

  

错误:   “条件中的模式匹配需要case关键字”   “使用未解决的标识符rootViewController

我已经追溯了这本书,确保标识符在主情节提要中匹配(尽管TabbarViewController没有课程,这是正常现象吗?)

func openSignInScreen() {
    if let signInViewController = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "SignInViewController") as? SignInViewController {
        signInViewController.view.frame = (self.window?.rootViewController?.view.frame)!
        signInViewController.view.layoutIfNeeded()
        //nice transition between views
        UIView.transition(with: window!, duration: 0.3, options: .transitionCrossDissolve, animations: {self.window?.rootViewController = signInViewController }, completion: { completed in //nothing to do here
        })
        }
    }
//continue.. now open main View Controller
func openMainViewController() {
    if let rootViewController() = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "TabbarViewController") {
            rootViewController().view.frame =  (self.window?.rootViewController?.view.frame)!
            rootViewController().view.layoutIfNeeded()
            //nice transition between views
            UIView.transition(with: window!, duration: 0.3, options: .transitionCrossDissolve, animations: {self.window?.rootViewController = rootViewController() }, completion: { completed in //maybe do something here
        })
    }
}

我希望我编写的代码与书中的代码相同,所以不会出现任何标识问题。但是,我在openMainViewController()函数上遇到了很多错误,而且我对编程总体上还是很陌生的-至今还不知道该怎么去。

1 个答案:

答案 0 :(得分:0)

我怀疑您在复制书本时出错了

rootViewController()替换为rootViewController,因为这应该是常量而不是方法。就像在openSignInScreen()

中一样

因此您的更新后的openMainViewController

func openMainViewController() {
        if let rootViewController = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "TabbarViewController") {
            rootViewController.view.frame =  (self.window?.rootViewController?.view.frame)!
            rootViewController.view.layoutIfNeeded()
            //nice transition between views
            UIView.transition(with: window!, duration: 0.3, options: .transitionCrossDissolve, animations: {self.window?.rootViewController = rootViewController }, completion: { completed in //maybe do something here
            })
        }
    }

注意:为此,您应该在故事板上有一个标识符为TabbarViewController