登录屏幕中的“记住用户”不会定向到 Swift 中的主屏幕。 (窗口为零)

问题描述 投票:0回答:1

登录后我想记住用户并且应该转到主 ViewController 但事实并非如此。我试图检查我的代码是否有效,它似乎有效,但它什么也没做。我知道它正在工作,因为当我编写“with Identifier”字符串时,它会立即给出错误,但是如果我写错“with Identifier”字符串,则会给出错误。我把打印放在理解中,它总是说“窗口为零”。顺便说一下,我在应用程序委托中做了:

 var window: UIWindow?

这是我的代码:

    let user : String? = UserDefaults.standard.string(forKey: "username")
    
    if user != nil {
        
        let board : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        
        let myTag = board.instantiateViewController(withIdentifier: "myTags") as! mainBeaconList
        
        
        print("mainBeaconList: \(myTag)")

        if let window = window {
            print("window: \(window)")
            window.rootViewController = myTag
        } else {
            print("window is nil")
        }
        
        
    }
swift xcode uiwindow
1个回答
0
投票

您可能缺少设置窗口

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            self.window = UIWindow(frame: UIScreen.main.bounds)
let user : String? = UserDefaults.standard.string(forKey: "username")
    
    if user != nil {
        
        let board : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        
        let myTag = board.instantiateViewController(withIdentifier: "myTags") as! mainBeaconList
        
        
        print("mainBeaconList: \(myTag)")

        if let window = window {
            print("window: \(window)")
            window.rootViewController = myTag
        } else {
            print("window is nil")
        }
    
    }
© www.soinside.com 2019 - 2024. All rights reserved.