4

I have a label which displays the current time in 12-hour format.

But, it does not update/refresh the time every time the minute/hour changes.

I need it to automatically change the label to the current time when the time changes.

reecefox
  • 47
  • 1
  • 1
  • 5

2 Answers2

13

Swift 3 Solution

class ViewController {

    @IBOutlet weak var timeLabel: UILabel!
    var timer: Timer?

    let formatter: DateFormatter = {
        let tmpFormatter = DateFormatter()
        tmpFormatter.dateFormat = "hh:mm a"
        return tmpFormatter
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        timer = Timer.scheduledTimer(timeInterval: 60, target: self, selector: #selector(self.getTimeOfDate), userInfo: nil, repeats: true)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        timer?.invalidate()
    }

    func getTimeOfDate() {
        var curDate = Date()

        timeLabel.text = formatter.string(from: curDate)
    }

}
pkamb
  • 33,281
  • 23
  • 160
  • 191
Ganesh Kumar
  • 1,631
  • 2
  • 21
  • 35
  • @reecefox check my answer – Ganesh Kumar Jan 23 '17 at 05:31
  • Please use lower case names for variables, constants, properties and cases. – Nicolas Miari Jan 23 '17 at 05:37
  • Is it necessary to set timer to nil after call `invalidate()`? – Yakiv Kovalskyi Mar 24 '17 at 09:38
  • @GaneshKumar but doesn't `invalidate()` free memory already? Is there any reason to call `timer = nil` after `invalidate()`? I don't think you're obliged to nullify Timer instance under ARC. – Yakiv Kovalskyi Mar 24 '17 at 10:13
  • sorry.. i mis understood it.. it doesnt need to be added – Ganesh Kumar Mar 25 '17 at 05:25
  • newbie here. Xcode says `NSTimer` should be renamed `Timer`. Thanks for this answer! – subelsky May 26 '17 at 18:07
  • 3
    This is not very good. It starts in `viewDidLoad` and ends in `viewWillDisappear`. This means it will stop when you navigate or press the home-button, and won't restart when you get back in there. If using `viewWillDisappear` you should use `viewWillAppear` to start it. Or use `deinit` if using `viewDidLoad`. – Sti Jun 23 '17 at 20:37
1

Replace following code with your ViewController code then connect IBOutlet to showTimeLbl

class ViewController: UIViewController {

    @IBOutlet weak var showTimeLbl: UILabel!
    var timeCount:Int = 0
    var timer:Timer!

    override func viewDidLoad() {
        super.viewDidLoad()

        if(timer != nil)
        {
            timer.invalidate()
        }
        timer = Timer(timeInterval: 1.0, target: self, selector: #selector(ViewController.timerDidFire), userInfo: nil, repeats: true)
        RunLoop.current.add(timer, forMode: RunLoopMode.commonModes)

    }

    func timerDidFire()
    {
        timeCount += 1

        showTimeLbl.text = NSString(format: "%02d:%02d:%02d", timeCount/3600,(timeCount/60)%60,timeCount%60) as String
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(true)
        timer?.invalidate()
        timer = nil
    }

}
pkamb
  • 33,281
  • 23
  • 160
  • 191
Jugal K Balara
  • 917
  • 5
  • 15