0

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.

Khadija Daruwala
  • 1,185
  • 3
  • 25
  • 54

4 Answers4

3
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)
}
Tony
  • 542
  • 3
  • 13
1

Well, iOS does not support toasts, so you must be using some kind of a library. I guess it does not support completion handlers, so the most straight forward thing to do is to wait for a moment and then present the next view controller.

// it waits for 3 seconds and then presents the next view controller
let delay = Int64(3 * Double(NSEC_PER_SEC))
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay), dispatch_get_main_queue()) { 
    let nextViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
    self.navigationController?.pushViewController(nextViewController, animated: true)
}
1

try this

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5.0 * NSEC_PER_SEC), dispatch_get_current_queue(), {() -> Void in
 let nextViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
self.navigationController?.pushViewController(nextViewController, animated: true)
})
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0
view.makeToast(message: "Success", duration: 2, position: HRToastPositionCenter, title: "Success")
                let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 2 * Int64(NSEC_PER_SEC))
                dispatch_after(time, dispatch_get_main_queue()) {
                    self.navigationController?.popViewControllerAnimated(true)
                     }
shan
  • 136
  • 7