Create Simple Stopwatch with NSTimer swift

This tutorial has been updated for X code 7.3 & swift 2.2

1. First lets set up the x code project as usual. Create new single view application X code project. Set the values as follows and save it on your desktop.

set project title to counter


2. Go to main storyboard and select view controller. Set its size iPhone 4-inch in attribute section.

set size to iPhone 4 inch


3. Add one label & three buttons as follows


add buttons & label

4. Change values of label & button as per the image

change buttons & label


5. Add all the buttons & label to view controller swift file

Screen Shot 2015-03-01 at 3.36.38 pm

Screen Shot 2015-03-01 at 3.35.13 pm

Screen Shot 2015-03-01 at 3.34.44 pm

Screen Shot 2015-03-01 at 3.33.48 pm

6. In view controller create “count” variable to keep track of count. And we instantiate timer variable here so we can just use same timer in all of the buttons.

class ViewController: UIViewController {
    // 6
    var count = 0
    var timer = NSTimer()

7. In start button add following code

    @IBAction func startPressed(sender: UIButton) {
        // 7
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(ViewController.counter), userInfo: nil, repeats: true)

This code will be triggered every 1 second & it will trigger “counter” function. And this cycle will repeat infinitely. If you want to trigger the code every 10 seconds then replace 1 by 10. And if you want timer to stop after one instance then make repeat “false”.

8. Now we have to define “counter” function as follows

    // 8
    func counter() {
        count += 1
        counterLabel.text = "\(count)"

After every 1 second the value of count is increased by 1. ++Count is shorthand. To see list of short hand go here.
Testing of app:Now run the app in iPhone 5s. And click on start button. If your label text does not change & increase in value. Then start over & see where you have made mistake.

9. If everything is working fine then we add small if else loop to make it look good aesthetically.

    func counter() {
        count += 1
        // 9
        if count > 9 {
            counterLabel.text = "\(count)"
        else {
            counterLabel.text = "0\(count)"

10. Now run the app & press start button. See if the app looks good. Then add following code in pause button.

    @IBAction func pausePressed(sender: UIButton) {
        // 10

Invalidate function pauses the timer. Now run the app & press start. Then text the pause button. After that press start again.

11. If the pause button is working properly then add following code in reset button.

    @IBAction func resetPressed(sender: UIButton) {
        // 11
        count = 0
        counterLabel.text = "00"

Here we are basically pausing the timer & resetting its value to zero. Now run the app in iPhone 5s & test all buttons.

Screen Shot 2015-03-01 at 7.50.26 pm
Download X Code file

Challenge 1 To create hour, minute & second label in this counter.

Download Challenge 1 solution

Challenge 2 Along with hour minute & second label create count down instead of count up.

Download Challenge 2 solution
Total 10 Votes

Tell us how can I improve this tutorial?

+ = Verify Human or Spambot ?