ios - 快速导航 View Controller 时倒计时计时器不起作用?

标签 ios swift countdowntimer stopwatch

当我最小化应用程序计时器时,倒计时在后台进行得很顺利。但是当我将 FirstViewController 导航到 SecondViewController 并从 SecondViewController 单击后退按钮转到 firstViewController 时,计时器会不继续倒计时。我想,当我回到第一个 View Controller 时,计时器仍在倒计时。

这是我的代码

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)
        }

}

这是我在 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
    }
}

第二个 View Controller :

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)
    }

}

这是 Storyboard中的 ImageView Controller Click To See Image

最佳答案

@IBAction func backToFirstScreen(_ sender: UIButton) 
{
   navigationController?.popViewController(animated:true)
}

您没有返回,而是创建了另一个第一个 View Controller 对象,该对象启动了另一个计时器。这是我在您的代码中看到的唯一问题

关于ios - 快速导航 View Controller 时倒计时计时器不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55557261/

相关文章:

ios - iPhone Xs - 使用 AVCaptureVideoPreviewLayer 时,为什么 UIView 的顶部边框和安全区域的顶部边框之间有巨大的填充?

ios - slider 在 Swift 中不与音频一起移动

ios - 在 Tab Swift 中使用页面 View Controller

java - 倒计时器显示 toast 一半时间

iphone - UIScrollView 的 contentOffset

ios - Swift 3 - 准备转场

ios - Firebase:使用 swift3 访问子快照

Swift 3 #selector 和类型名称

java - Android Java 取消 CountDownTimer 的问题

python - 显示 python sleep 函数的倒计时