I wish to auto-segue from the main viewController to a second view controller after a set period of time after an app loads.
How do I do this?
Do I need to do it programmatically?
If your UI is laid out in a Storyboard, you can set an NSTimer
in viewDidLoad
of your first ViewController
and then call performSegueWIthIdentifier
when the timer fires:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let timer = Timer.scheduledTimer(interval: 8.0, target: self, selector: #selector(timeToMoveOn), userInfo: nil, repeats: false)
}
@objc func timeToMoveOn() {
self.performSegue(withIdentifier: "goToMainUI", sender: self)
}
Here is how you set up the segue in the Storyboard:
ViewController
to the second ViewController
.You can use this snippet of code:
let delay = 1 // Seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay * NSEC_PER_SEC)), dispatch_get_main_queue()) {
self.launchMainUI()
return
}
which performs the launchMainUI
method after delay
seconds. Replace it with your own implementation, where you instantiate your view controller and present it, or simply invoke a segue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With