Search code examples
iosswifttoast

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.


Solution

  • 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)
    }