Swift 3/Xcode 8.2 - 在 viewDidLoad 中切换视图不起作用 [英] Swift 3 / Xcode 8.2 - switch view in viewDidLoad not working
本文介绍了Swift 3/Xcode 8.2 - 在 viewDidLoad 中切换视图不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当用户已经登录时,我想在加载时切换 ViewController.问题:当用户等于true"时,视图没有改变......
I want to switch the ViewController on load when the user is already logged in. The problem: The view didnt change when user is equal to "true"...
提前谢谢你!!
override func viewDidLoad() {
super.viewDidLoad()
var user: String?
user = UserDefaults.standard.value(forKey: "loginSuccessfull") as? String
if user == "true" {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "SecondView") as UIViewController!
self.show(vc!, sender: vc)
}
// Do any additional setup after loading the view, typically from a nib.
}
推荐答案
从 viewDidLoad
方法中删除代码并使用下面的方法.
Remove the code from viewDidLoad
method and use the below method.
override func viewDidAppear() {
guard let user = UserDefaults.standard.string(forKey: "loginSuccessfull"), user == "true" else {
return
}
let vc = self.storyboard?.instantiateViewController(withIdentifier: "SecondView")!
self.present(vc, animated: true)
}
这篇关于Swift 3/Xcode 8.2 - 在 viewDidLoad 中切换视图不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文