登录后我想记住用户并且应该转到主 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")
}
}
您可能缺少设置窗口
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")
}
}