user8105388
user8105388

Reputation:

use uitextfield to stop timer (swift4)

I want the user the to enter a number in the textfield and when the timer hits that number the timer stops. I am trying it right now but its saying I can't use boolean operators to do this.

   import UIKit
class ViewController: UIViewController {
@IBOutlet var playbutton: UIButton!
@IBOutlet var titlelabel: UILabel!

    @IBOutlet var judo: UITextField!

var timer : Timer?
var counter = 0.0
var isRunning = false


override func viewDidLoad() {
    super.viewDidLoad()
    titlelabel.text = "\(counter)"
    playbutton.isEnabled = true

}

@IBAction func btnplay(_ sender: UIButton) {
    if timer == nil {
        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(UpdateTime), userInfo: nil, repeats: true)
        playbutton.isEnabled = false
    }
}
@objc func UpdateTime(){
    counter += 0.1
    titlelabel.text = String(format: "%.1f", counter)
    if counter >= (Int(judo.text!)) {
        timer?.invalidate()
        timer = nil
    }



    }

    }

Upvotes: 0

Views: 47

Answers (1)

Bilal
Bilal

Reputation: 19156

You can't use binary operator with operands of type Double and Int. counter is Double so the right operand must be Double

let max = Double(judo.text ?? "") ?? 0.0
if counter >= max {
    timer?.invalidate()
    timer = nil
}

Upvotes: 1

Related Questions