-1

I need to add a global timer of 60 minutes in the application which will show in all ViewControllers. But not able to implement this feature.

I have implemented timer in one ViewController till now. Here is my code:

 var timerCount = 3600
var timer = Timer()
 timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(update), userInfo: nil, repeats: true)

func update() {

    if(timerCount > 0){
        let minutes = String(timerCount / 60)
        let seconds = String(timerCount % 60)
        timeLbl.text = minutes + ":" + seconds
        timerCount -= 1
    }
    else {
        timer.invalidate()
        let vc = storyboard?.instantiateViewController(withIdentifier: "NavigateViewController")
        navigationController?.pushViewController(vc!, animated: true)

    }

}

I need to show this timer value in every ViewController of the application.

3 Answers3

2
// Create  class TimerManager

class TimerManager {
    var timerCount = 3600

       static let sharedInstance = TimerManager()
    var timer: Timer?

    init() {

        self.timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(update), userInfo: nil, repeats: true)


    }

    @objc func update() {
        if(timerCount > 0){
            let minutes = String(timerCount / 60)
            let seconds = String(timerCount % 60)
            timerCount -= 1
        }
        else {
            self.timer?.invalidate()


        }
    }


}


// At view Controller   you can access  timerCount

  print(TimerManager.sharedInstance.timerCount)
Abdelahad Darwish
  • 5,969
  • 1
  • 17
  • 35
0

You should display it in a separate UIWindow instance. Put the label in a separate View Controller and refer to answer 42 from this post: To create a new UIWindow over the main window

Arik Segal
  • 2,963
  • 2
  • 17
  • 29
0
// Create  class TimerManager

class TimerManager {
    var timerCount = 3600

       static let sharedInstance = TimerManager()
    var timer: Timer!

    init() {

        self.timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(update), userInfo: nil, repeats: true)


    }

    @objc func update() {
        if(timerCount > 0){
            let minutes = String(timerCount / 60)
            let seconds = String(timerCount % 60)
            timerCount -= 1
        }
        else {
            self.timer?.invalidate()


        }
    }


}


// At view Controller   you can access  timerCount

  print(TimerManager.sharedInstance.timerCount)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • Will this be a NSObject class? And how to call method to start timer ? – vickydutta Aug 20 '17 at 12:23
  • Welcome to SO!. You might want to explain how your code solves the problem for OP. I suggest you edit your post and improve it. – Suraj Rao Aug 20 '17 at 13:09
  • you've missed the most important part: how to update the VC's with the current time. @vickydutta, you can have the `update` method send a notification and have all the VC register for it. when a VC gets a notification from the timer it is updating the timeLbl. – gutte Aug 20 '17 at 14:06