How to make a simple countdown in minutes for swift iOS?

16,540

Look into NSTimer as a simple way to do this. May not be the most accurate but is relatively simple. The NSTimer calls the function "update" every second (indicated by the 1.0 as the first argument)

@IBOutlet var countDownLabel: UILabel!

var count = 300

override func viewDidLoad() {
    super.viewDidLoad()

    var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}

func update() {

    if(count > 0){
        let minutes = String(count / 60)
        let seconds = String(count % 60)
        countDownLabel.text = minutes + ":" + seconds
        count--
    }

}
Share:
16,540
Admin
Author by

Admin

Updated on June 18, 2022

Comments

  • Admin
    Admin almost 2 years

    How do I code a simple timer countdown from MINUTES say for example 5 minute countdown?

    Like the timer that apple created. Is there a way to download and see the xcode project for apple apps?

  • Abdul Waheed
    Abdul Waheed almost 8 years
    thank you that solved my issue