xcode 登录屏幕中的“记住用户”未指向Swift中的主屏幕,(窗口为空)

ru9i0ody  于 2023-02-16  发布在  Swift
关注(0)|答案(1)|浏览(82)

登录后,我想记住用户,应该去主ViewController,但它没有。我试图检查我的代码是否工作,它似乎工作,但它什么也不做。我知道它是工作,因为当我写字符串“with Identifier”,它会立即给出错误,但如果我写“with Identifier”字符串错误,它会给出错误。我把print设置为“understanding”,它会说“window is nil”所有的时间。顺便说一下,在应用程序委托中,我做了:

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

zxlwwiss1#

您可能没有设置窗口

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        self.window = UIWindow(frame: UIScreen.main.bounds)
//Rest of Initialization

}

相关问题