我有一个功能,我检查是否设置了UserDefaults,如果没有,则会打开一个新的View Controller并显示一个登录屏幕,用于设置用户默认值。
我的问题是视图控制器没有实例化但我得到一个打印“用户未注册”
func checkUserAccount() {
let defaults = UserDefaults.standard
let accountName = defaults.bool(forKey: "PUserAccountName")
let accountPassword = defaults.bool(forKey: "PUserAccountPassword")
if accountName == true && accountPassword == true {
print("User Registered")
} else {
let storyboard: UIStoryboard = UIStoryboard(name: "PolTRiM", bundle: nil)
let vc: StudentLoginVC = storyboard.instantiateViewController(withIdentifier: "studentLogin") as! StudentLoginVC
vc.modalPresentationStyle = .custom
vc.modalTransitionStyle = .crossDissolve
self.present(vc, animated: true, completion: { _ in })
print("User not registered")
}
}
有什么想法吗?
答案 0 :(得分:1)
如果正确写入了UIStoryBoard
名称和UIViewController
标识符,您是否仔细检查过?否则此代码对我有用
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"MyViewController") as! UIViewController
self.present(viewController, animated: true)