I am testing a simple login system for an iOS app.
This is the code where I want to check if the user was logged in previosly:
override func viewDidLoad() {
super.viewDidLoad()
let logeado = UserDefaults.standard.string(forKey: "logeado")
let men = "SI"
if logeado == men {
showToast(message: logeado!)
self.performSegue(withIdentifier: "entrar_segue", sender: self)
}
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "entrar_segue"{
var vc = segue.destination as! HomeViewController
}
}
The Toast is shown, but the segue is not performed. There is a button that uses the same function:
self.performSegue(withIdentifier: "entrar_segue", sender: self)
and there it works.
What is wrong in the code?
You need to call this self.performSegue
method in dispatch_async
block:
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
self.performSegue(withIdentifier: "seg", sender: self)
}
}
That's because viewDidLoad()
runs in background thread and all UI updates and transitions must be done on Main Thread.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.