I need help on what to do here . I have a code that runs one clock that count downs from 10 sec. 0, but I need three more clocks just like it that runs individually when press start button just like hockey penalty clocks. I don't want to copy and past codes all day. I know there's got be better way to code then what I am doing. Any ideas? I am new at Xcode and first time posting here.
Thank you
woo
Thank you
woo
Code:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var Clock1L: UILabel!
var seconds = 10
var timer = Timer()
var timerIsOn = false
@IBAction func startButton1(_ sender: AnyObject) {
if timerIsOn == false {
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self,selector:#selector(ViewController.updateTimer), userInfo: nil, repeats: true)
timerIsOn = true
}
}
@IBAction func stopButton1(_ sender: AnyObject) {
timer.invalidate()
Clock1L.text = "\(seconds)"
timerIsOn = false
}
@IBAction func resetButton1(_ sender: AnyObject) {
timer.invalidate()
seconds = 10
Clock1L.text = "\(seconds)"
timerIsOn = false
}
func updateTimer() {
seconds -= 1
Clock1L.text = "\(seconds)"
if seconds == 0 {
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.
}
}
Last edited by a moderator: