简体   繁体   English

无法实例化View Controller

[英]Unable to Instantiate View Controller

I have a function, whereby I check if the UserDefaults are set and if not a new View Controller opens and presents a login screen which will set the user defaults. 我有一个函数,通过它可以检查是否设置了UserDefaults,如果没有,则打开一个新的View Controller并显示一个登录屏幕,该屏幕将设置用户默认值。

My problem is the view controller does not Instantiate but I get a print "User not registered" 我的问题是视图控制器没有实例化,但是我得到打印“用户未注册”

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")
        }

    }

Any thoughts? 有什么想法吗?

Have you double checked UIStoryBoard name and UIViewController identifier if it's written correctly? 您是否仔细检查过UIStoryBoard名称和UIViewController标识符(如果编写正确)? Otherwise this code is working for me 否则这段代码对我有用

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"MyViewController") as! UIViewController
self.present(viewController, animated: true)    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM