Suraj Sharma
Suraj Sharma

Reputation: 9

i am tying to work timer through app

I am working on an app that requires timing. The timer activates in one View Controller and it needs to continue until the last View Controller or resign the app. For example, I have2 View Controllers and the timer need to start in second View Controller at 5 minutes, then the user starts using the rest four View Controllers. When the user reaches the first or resign app View Controller, then the timer should needs to continue .but timer goes reset .he started again from starting point /

Here is my code below :

viewcontroller 1

@IBAction func nextvc(_ sender: UIButton){
present(animated:true)
}

viewcontroller 2

@IBAction func checkin(_ sender: UIButton)
    {
func timercall() {
        var time = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(calltime), userInfo: nil, repeats: true)
    }}

Upvotes: 0

Views: 38

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You must make the timer global for 5 mins do this

 sharedInstance.time = Timer.scheduledTimer(timeInterval: 1 , target: self, selector: #selector(calltime), userInfo: nil, repeats: true)

Edit:

try to cash the current second in timer call back and when app comes to foreground subtract it from total num of seconds

Upvotes: 0

Related Questions