<span style="font-size:18px;">时间控制器NSTimer可以实现定时器功能,即每隔一定时间执行具体函数,可以重复也可以只执行一次。</span>
class DownloadManagerTableTableViewController: UITableViewController {
var timer : NSTimer?
override func viewDidLoad() {
super.viewDidLoad()
}
//页面显示之前设定定时器
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.timer = NSTimer.scheduledTimerWithTimeInterval(0.5,target: self,selector: "timerFired",userInfo: nil,repeats: true)
}
//页面消失后关闭定时器
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
guard let timer = self.timer
else { return }
timer.invalidate()
}
func timerFired () {
//print("timer ticked")
self.tableView.reloadData()
}
}
原文链接:https://www.f2er.com/swift/324407.html