PersianBlue PersianBlue - 4 months ago 28
Swift Question

How to make a toast and then change the screen at the same button click ios swift

I am trying to show a toast when the user hits the register button and then direct the user to the login screen as follows:

print("Registration Successful")
self.view.makeToast("Registration Successful")
let nextViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
self.navigationController?.pushViewController(nextViewController, animated: true)


But the problem here is that screen changes quickly due to which the toast can hardly be read. I would like to show the "Successful" toast and then change the screen. Can someone help me with it.

Answer
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
    self.view.hideToast()
    let nextViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
    self.navigationController?.pushViewController(nextViewController, animated: true)
}