Reputation: 15
The countdown goes well in the background, when I minimize the app timer still countdown. but when I navigate FirstViewController
to the SecondViewController
and click back button from SecondViewController
to go to firstViewController
, the timer does not continue countdown. I want, when I am back to the first view controller the timer is still countdown.
here is my code
import UIKit
class FirstViewController: UIViewController {
@IBOutlet weak var timerLabel: UILabel!
var countdownTimer: Timer!
var totalTime = 200
override func viewDidLoad() {
super.viewDidLoad()
startTimer()
}
func startTimer() {
countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}
@objc func updateTime() {
timerLabel.text = "\(timeFormatted(totalTime))"
if totalTime != 0 {
totalTime -= 1
} else {
endTimer()
}
}
func endTimer() {
countdownTimer.invalidate()
}
func timeFormatted(_ totalSeconds: Int) -> String {
let seconds: Int = totalSeconds % 60
let minutes: Int = (totalSeconds / 60) % 60
// let hours: Int = totalSeconds / 3600
return String(format: "%02d:%02d", minutes, seconds)
}
@IBAction func nextScreen(_ sender: UIButton) {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController")
navigationController?.pushViewController(vc, animated: true)
}
}
and here is my code in AppDelegate to make timer still run in background:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var backgroundUpdateTask: UIBackgroundTaskIdentifier = UIBackgroundTaskIdentifier(rawValue: 0)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
func applicationWillResignActive(_ application: UIApplication) {
self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
self.endBackgroundUpdateTask()
})
}
func applicationDidEnterBackground(_ application: UIApplication) {
}
func applicationWillEnterForeground(_ application: UIApplication) {
self.endBackgroundUpdateTask()
}
func endBackgroundUpdateTask() {
UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
self.backgroundUpdateTask = UIBackgroundTaskIdentifier.invalid
}
func applicationDidBecomeActive(_ application: UIApplication) {
application.applicationIconBadgeNumber = 0
}
}
second ViewController:
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func backToFirstScreen(_ sender: UIButton) {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FirstViewController")
navigationController?.pushViewController(vc, animated: true)
}
}
this the image view controller in storyboard
Upvotes: 1
Views: 620
Reputation: 1909
Its because you are not getting back to firstViewController!
You are creating a new instance of firstViewController and pushing it on secondViewController.
For getting back you can pop the secondViewController:
@IBAction func backToFirstScreen(_ sender: UIButton) {
self.navigationController?.popViewController(animated: true)
}
Upvotes: 1
Reputation: 21808
@IBAction func backToFirstScreen(_ sender: UIButton)
{
navigationController?.popViewController(animated:true)
}
Instead of going back you create another first view controller object which starts another one timer. This is the only issue that I can see in your code
Upvotes: 1