simple timer in swift 4





import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var timerLabel: UILabel!
    var timer = Timer()
    var counter = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        timerLabel.text = String(counter)
       
        // Do any additional setup after loading the view, typically from a nib.
    }


    @IBAction func startBtnPressed(_ sender: Any) {
        
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
        
    }
    
    
    @IBAction func pauseBtnPressed(_ sender: Any) {
        timer.invalidate()
    }
    
    
    @IBAction func restartBtnPressed(_ sender: Any) {
        counter = 0
        timer.invalidate()
        timerLabel.text = String(counter)
    }
    
    
    
   @objc func updateTimer() {
        counter = counter+1
        timerLabel.text = String(counter)
        
    }

}


Previous
Next Post »