导航视图控制器swift时,CountDown计时器不起作用?

问题描述 投票:1回答:2

倒数在后台运行良好,当我最小化应用程序计时器仍然倒计时。但是当我将FirstViewController导航到SecondViewController并从SecondViewController点击后退按钮去firstViewController时,计时器不会继续倒计时。我想,当我回到第一个视图控制器时,计时器仍然是倒计时。

这是我的代码

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

第二个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)
    }

}

这是故事板中的图像视图控制器

ios swift countdowntimer stopwatch
2个回答
1
投票
@IBAction func backToFirstScreen(_ sender: UIButton) 
{
   navigationController?.popViewController(animated:true)
}

而不是返回,你创建另一个第一个视图控制器对象,启动另一个计时器。这是我在代码中可以看到的唯一问题


1
投票

因为你没有回到firstViewController!

您正在创建firstViewController的新实例并将其推送到secondViewController。

要获得回来,您可以弹出secondViewController:

@IBAction func backToFirstScreen(_ sender: UIButton) {

    self.navigationController?.popViewController(animated: true)
}
© www.soinside.com 2019 - 2024. All rights reserved.