Timer class, not increasing count - swift

I'm not sure where I did wrong. I don't see any error, but the count isn't increasing.
#IBOutlet weak var myTimerLabel: UILabel!
let myTimer = Timer()
#objc func myTestFunc() {
var count = 0
count+=1
myTimerLabel.text = String(count)
}
override func viewDidLoad() {
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector
(myTestFunc), userInfo: nil, repeats: true)
}

Its not increasing because each time myTestFunc() called, it reinitialize the count variable with zero. So, declare count variable outside myTestFunc().

You have to make structure in right way
check this:
#IBOutlet weak var myTimerLabel: UILabel!
let myTimer = Timer()
var count = 0
//MARK: - view did load
override func viewDidLoad() {
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector (myTestFunc), userInfo: nil, repeats: true)
}
//TODO:- here implement your funtion
#objc func myTestFunc() {
count+=1
myTimerLabel.text = String(count)
// if you want to stop your timer
myTimer.invalidate()
}
Good luck

Related

xcode 10.1: Timer stops at 1 not 0

Yep, I have seen a few questions similar to mine but for some reason, I couldn't duplicate it! If I use == or <= with If both are giving me stop at 1. The only < gave me the 0 stops which are good, but very rare I got -1. Any help, please?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var timeLabel: UILabel!
#IBOutlet weak var startButton: UIButton!
var timer = Timer()
var timeLeft = 10
override func viewDidLoad() {
timeLabel.text = String(10)
super.viewDidLoad()
}
#IBAction func pressTimer(_ sender: Any) {
startButton.isEnabled = false
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(startTime), userInfo: nil, repeats: true)
}
#objc func startTime() {
timeLeft -= 1
timeLabel.text = "\(timeLeft)"
if timeLeft < 0 {
startButton.isEnabled = false
self.timer.invalidate()
}
}
}
My suggestion is to declare the timer as optional and check for zero before decrementing the counter.
And set the counter to 10 in the pressTimer method
class ViewController: UIViewController {
#IBOutlet weak var timeLabel: UILabel!
#IBOutlet weak var startButton: UIButton!
var timer : Timer?
var timeLeft = 0
#IBAction func pressTimer(_ sender: Any) {
startButton.isEnabled = false
timeLeft = 10
timeLabel.text = String(timeLeft)
if timer == nil {
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(startTime), userInfo: nil, repeats: true)
}
}
#objc func startTime()
{
if timeLeft == 0 {
startButton.isEnabled = false // shouldn't it be true??
timer?.invalidate()
timer = nil
} else {
timeLeft -= 1
timeLabel.text = String(timeLeft)
}
}
}

Textfield didChange with timer

I'm working on autocompletion in my project and I would like to detect when the textfieldDidChange value and call a method (link to API) 500MS after that.
I hope it's clear enough
Thank for your help !
In Swift 3, you probably want to connect to "editing changed" not "value changed", and reset the timer and start another timer:
weak var timer: Timer?
#IBAction func didChangeEditing(_ sender: UITextField) {
timer?.invalidate()
timer = .scheduledTimer(withTimeInterval: 0.5, repeats: false) { [weak self] timer in
// trigger your autocomplete
}
}
Or you can alternatively hook into shouldChangeCharactersIn. For example:
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self // or you can do this in IB
}
weak var timer: Timer?
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
timer?.invalidate() // cancel prior timer, if any
timer = .scheduledTimer(withTimeInterval: 0.5, repeats: false) { [weak self] timer in
// trigger your autocomplete
}
return true
}
}
First make sure to make your class the TextField's delegate:
class yourClass: UIViewController, UITextFieldDelegate{
//...
}
And then in viewDidLoad():
textField.delegate = self
numbersTextField.addTarget(self, action: #selector(self.textFieldDidChange), for: .editingChanged)
After that, you can use a timer, like this:
var timer = Timer()
var count = 0
func textFieldDidChange(textField: UITextField){
timer.invalidate()
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.timerFunc), userInfo: nil, repeats: true)
}
func timerFunc(){
count += 1
if count == 5{
timer.invalidate()
// do your things here down here, I assumed that your method is called autocomplete
autocomplete()
}
}
Hope it helps!

Stopwatch code Issue

I am trying to create a stopwatch app, for some reason when I press the start button in the app, instead of it going, 1,2,3,4,5 etc. It shows this '<'. I have gone over the code but I can find nothing.
class ViewController: UIViewController {
var timer = NSTimer()
var time = 0
func result() {
time + 1
timeLabel.text = "\(timer)"
}
#IBOutlet var timeLabel: UILabel!
#IBAction func stop(sender: AnyObject) {
timer.invalidate()
time = 0
timeLabel.text = "0"
}
#IBAction func timeButton(sender: AnyObject) {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
}
}
You have a typo: timeLabel.text = "\(timer)" should be timeLabel.text = "\(time)".
Also, time + 1 should be time += 1. With those changes, the following code works:
class ViewController: UIViewController {
var timer = NSTimer()
var time = 0
func result() {
time += 1
timeLabel.text = "\(time)"
}
#IBOutlet var timeLabel: UILabel!
#IBAction func stop(sender: AnyObject) {
timer.invalidate()
time = 0
timeLabel.text = "0"
}
#IBAction func timeButton(sender: AnyObject) {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(result), userInfo: nil, repeats: true)
}
}

Building a small timer app, why won't app timer stop both labels?

I'm in the process of building a timer app
I have a button that starts the timer and changes both labels
I have another button that stops the timer with .invalidete.
the problem is when the timer starts both labels change at the set rate they are suppose to
But when I hit the stop button only the 2nd timer label stops changing, the 1st label seems to keep going I don't know why
import UIKit
class ViewController: UIViewController {
var timer = NSTimer()
var counter = 0
var counter2 = 0
#IBOutlet weak var label: UILabel!
#IBOutlet weak var label2: UILabel!
#IBAction func start(sender: AnyObject){
counter = 0
label.text = String(counter)
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "updateCounter", userInfo: nil, repeats: true)
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateCounter2", userInfo: nil, repeats: true)
}
func updateCounter2(){
counter2 += 1
label2.text = String(counter2)
}
func updateCounter(){
counter += 1
label.text = String(counter)
}
#IBAction func stop(sender: AnyObject) {
timer.invalidate()
timer.invalidate()
}
}
What you wan't to do is something like this:
var timer:NSTimer!
var timer2:NSTimer!
var counter = 0
var counter2 = 0
#IBOutlet weak var label: UILabel!
#IBOutlet weak var label2: UILabel!
#IBAction func start(sender: AnyObject){
counter = 0
label.text = String(counter)
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "updateCounter", userInfo: nil, repeats: true)
timer2 = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateCounter2", userInfo: nil, repeats: true)
}
func updateCounter2(){
counter2 += 1
label2.text = String(counter2)
}
func updateCounter(){
counter += 1
label.text = String(counter)
}
#IBAction func stop(sender: AnyObject) {
timer.invalidate()
timer2.invalidate()
}
Watch this talk in value types from WWDC 2015, it explains a couple of things about OOP that you might find useful.

NSTimer() - timer.invalidate not working on a simple stopwatch?

I'm kind of at a wall of what to do, the program runs, but the timer.invalidate() does not work? Any tips are welcome.
I worked with this program in the past and worked through it and the timer.invalidate() worked flawlessly. I do not believe that it has to do with the fact that I put it into a function because before it wasn't working when I was just typing "timer.invalidate()" instead of "stop()"
Whenever I click the button Cancel on the iOS simulator it just resets it back to 0, but keeps counting.
Thanks in advance.
import UIKit
class ViewController: UIViewController {
var timer = NSTimer()
var count = 0
#IBOutlet weak var timeDisplay: UILabel!
#IBAction func playButton(sender: AnyObject) {
//play button
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
}
#IBAction func resetTimer(sender: AnyObject) {
//cancel button
stop()
count = 0
timeDisplay.text = "0"
}
#IBAction func pauseButton(sender: AnyObject) {
stop()
}
func result() {
count++
timeDisplay.text = String(count)
}
func stop () {
timer.invalidate()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
In playButton() you are defining another timer, that can't be invalidated from outside this function - so by calling timer.invalidate() you invalidate just var timer = NSTimer() which doesn't carry any set timer in it.
So replace
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
with
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)