[英]CountDown timer didn't work when navigating the view controller swift?
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. 但是,当我浏览
FirstViewController
到SecondViewController
并单击后退按钮从SecondViewController
去firstViewController
,定时器不继续倒计时。 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: 这是我在AppDelegate中的代码,以使计时器仍在后台运行:
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: 第二个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 这是情节提要中的图像视图控制器
@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
这是我可以在您的代码中看到的唯一问题
Its because you are not getting back to firstViewController! 这是因为您没有回到firstViewController!
You are creating a new instance of firstViewController and pushing it on secondViewController. 您正在创建一个firstViewController的新实例,并将其推送到secondViewController上。
For getting back you can pop the secondViewController: 为了找回,您可以弹出secondViewController:
@IBAction func backToFirstScreen(_ sender: UIButton) {
self.navigationController?.popViewController(animated: true)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.