阅读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()
函数上遇到了很多错误,而且我对编程总体上还是很陌生的-至今还不知道该怎么去。
答案 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
的