I want to show a new ViewController after I clicked on a button, but not directly. After clicking the button I want to wait 3 seconds an after the 3 seconds I want the new ViewController
Add delay on click of button:
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
// Your code with navigate to another controller
}
You can use a scheduled timer to fire off a function after a given timeInterval.
let timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(showVC), userInfo: nil, repeats: false)
@objc func showVC() {
//navigate to your desired VC
}
DispatchQueue.main.asyncAfter(deadline: .now() + 3) { in [weak,self]
// Your code with navigate to another controller // weak self becouse may be your current viewcontroller is dinit
}
Inside the button action create a timer:
@IBAction func Button(_ sender: Any) {
_ = Timer(timeInterval: 3, target: self, selector: #selector(showVC), userInfo: nil, repeats: false)
}
Then add an objective-C selector that will present the next View Controller when called by the timer 3 seconds later.
@objc func showVC() {
let myVC = self.storyboard?.instantiateViewController(withIdentifier: "NextViewController") as! ViewController
self.present(myVC, animated: true, completion: nil)
}
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.