38

How do i stop my timer from running? Not like a pause, but a stop.

import UIKit

class LastManStandingViewController: UIViewController {    

@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var timeTextbox: UITextField!
@IBOutlet weak var startButton: UIButton!
@IBOutlet weak var stopButton: UIButton!

var myCounter = 0
var myTimer : NSTimer = NSTimer()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    timeLabel.text = String(myCounter)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func startTimer(){
    myTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTimer"), userInfo: nil, repeats: true)
    println("func startTimer")
}

func stopTimer(){
    myTimer.invalidate()
    myCounter = 0
    timeLabel.text = String(myCounter)
    println("func stopTimer")
}

func updateTimer(){
    timeLabel.text = String(myCounter++)
    println("func updateTimer")
}
@IBAction func startButton(sender: AnyObject) {
    startTimer()
}
@IBAction func stopButton(sender: AnyObject) {
    stopTimer()
}

}

I can start the timer, but when i press the Stop button, it reset itself, and starts counting again. It doesn't stop.

Made it work. Something was buggy with my project! Fixed it by removing the button and re-adding them. Looks like i had a duplicate or something.

Erik Auranaune
  • 1,384
  • 1
  • 12
  • 27
  • Please post your code (we can't test it if it's just a screenshot). – Eric Aya Apr 20 '15 at 12:59
  • 1
    @ericd Added full code now. – Erik Auranaune Apr 20 '15 at 13:01
  • I've copy-pasted the important bits of your code in an example app and I see no problems, the timer starts and stops correctly. Are you sure you're not doing anything else? [screenshot of my test](https://www.evernote.com/shard/s89/sh/ca19d729-0a27-421f-bd79-19cfa7c503c8/25d3926720f64e01c47ae0eaaaa7f3ee/deep/0/AppDelegate.swift.png) – Eric Aya Apr 20 '15 at 13:10
  • your code is working fine for me. – Dharmesh Kheni Apr 20 '15 at 13:13
  • So wierd. All I do is run the application, I press the start button, wait a few seconds and then press the stop button, and then close the application. This comes up: http://i.snag.gy/GrbFr.jpg – Erik Auranaune Apr 20 '15 at 13:15
  • http://gyazo.com/df23869027e79463a7800a31b313641f – Dharmesh Kheni Apr 20 '15 at 13:15
  • @ErikAuranaune Looking at your jpg, it almost looks like it starts a timer at the same time you stop this one. Are you sure nothing fires the `start` button when you hit the `stop` button or anything like that? Otherwise I don't know what's happening... – Eric Aya Apr 20 '15 at 13:20
  • 2
    @ericd OK, so I removed the start and stop buttons and added them again, now everything works fine. Wierd! – Erik Auranaune Apr 20 '15 at 15:24

3 Answers3

77

You don't have to use Selector:

@IBAction func startButton(sender: AnyObject) {
    myTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateTimer:", userInfo: nil, repeats: true)
}

Also, the timer passes itself to the selected method, so you can invalidate it inside the method if you need:

func updateTimer(timer: NSTimer) {
    timeLabel.text = String(Counter++)
    timer.invalidate()
}

Or if the timer is an instance variable:

myTimer.invalidate()
myTimer = nil

It's a good thing to nil the instance variable timer after having invalidated it, it avoids further confusion if you need to create another timer with the same variable. Also, method names and variables should begin with a lowercase letter.

Screenshot to show the timer invalidated and set to nil.

screenshot

Update for Swift 2.2+

See https://stackoverflow.com/a/36160191/2227743 for the new #selector syntax replacing Selector().

Community
  • 1
  • 1
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
7

you can use this when some condition is met and you want to stop timer:

Timer.invalidate()

Here is simple example :

func UpdateTimer(){
    timeLabel.text = String(Counter++)
    if timeLabel.text == String("5") {
        Timer.invalidate()
    }
}

this will stop timer.

You can modify it as per your need.

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
-3

As pre Swift 2.2

let printTimer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: #selector(printDescription), userInfo: nil, repeats: true)

func printDescription() {
    print("Print timer will print every after 2.0 seconds")
}

Print timer will print every after 2.0 seconds

Gautam Sareriya
  • 1,833
  • 19
  • 30