My code below uses a label and a variable that contains 30 seconds. The code counts down to zero then stops. I want to do the same thing just use the var texty as the textfield for the user to enter a number and countdown from that amount of seconds.
Hi @timswift,
You add a button and in the code for when it is pressed, you start the timer.
@IBAction func startTimer() {
// This is the most important bit here...
if let number = Int(texty.text) {
seconds = number
timer = Timer.ScheduledTimer(timeInterval: 1, target: self, ...)
} else {
// Display that the number in the text box is invalid or not a number
}
}
and it is a good habit to also nil the variable once you are done but that would mean using optionals (which you can skip for now).
Hi @timswift,
if you have copy pasted my code straight off, then you have to change my var timer to TIMER or as good variable naming conventions go, rename all of your instances of TIMER to timer
hi @timswift,
It’s a typo at my end, my keyboard is playing up. it should be scheduled not schedules
and the … is so that I don’t have to type the entire line, please use what you had in that line. timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(twoViewController.clocker), userInfo: nil, repeats: true