1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import UIKit class ViewController: UIViewController { @IBOutlet weak var timeLabel: UILabel! var timer = Timer() var counter = 0 override func viewDidLoad() { super.viewDidLoad() counter = 10 timeLabel.text = "Time: \(counter)" timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerFunction), userInfo: nil, repeats: true) } @objc func timerFunction() { timeLabel.text = "Time: \(counter)" counter -= 1 if counter == 0 { timer.invalidate() timeLabel.text = "Time's Over" } } @IBAction func buttonClicked(_ sender: Any) { print("button clicked") } } |