swift - How to add countdowntimer which will run in whole iOS application? -
i need add global timer of 60 minutes in application show in viewcontrollers. not able implement feature.
i have implemented timer in 1 viewcontroller till now. here 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 show timer value in every viewcontroller of application.
// 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() } } } // @ view controller can access timercount print(timermanager.sharedinstance.timercount)
Comments
Post a Comment