Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 指定标签不显示倒计时_Swift_Xcode_Timer_Countdown - Fatal编程技术网

Swift 指定标签不显示倒计时

Swift 指定标签不显示倒计时,swift,xcode,timer,countdown,Swift,Xcode,Timer,Countdown,我实现了倒计时,但当前指定用于显示值的标签根本没有显示值。我试着打印(countdownLabel),看看它是否是返回标签属性的录制,而不是倒计时。提前谢谢 class CountdownViewController: UIViewController { @IBOutlet weak var iconImage: UIImageView! @IBOutlet weak var countdownLabel: UILabel! //Countdown let

我实现了倒计时,但当前指定用于显示值的标签根本没有显示值。我试着打印(countdownLabel),看看它是否是返回标签属性的录制,而不是倒计时。提前谢谢

class CountdownViewController: UIViewController {

    @IBOutlet weak var iconImage: UIImageView!
    @IBOutlet weak var countdownLabel: UILabel!

    //Countdown
    let futureDate: Date = {
        let future = DateComponents(
            year: 2021,
            month: 1,
            day: 06,
            hour: 09,
            minute: 32 ,
            second: 45
        )
        return Calendar.current.date(from: future)!

    }()

    var countdown: DateComponents {
        return Calendar.current.dateComponents([.day, .hour, .minute, .second], from: Date(), to: futureDate)
    }

    @objc func updateTime() {
        let countdown = self.countdown //only compute once per call
        let days = countdown.day!
        let hours = countdown.hour!
        let minutes = countdown.minute!
        let seconds = countdown.second!
        self.countdownLabel.text = String(format: "%02d:%02d:%02d:%02d", days, hours, minutes, seconds)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        func runCountdown() {
            Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
        }
        runCountdown()
        (print(countdown))
        (print(countdownLabel as Any))
    }
}

代码中的问题是,您在另一个函数中声明了函数
runCountdown()
,但之后没有调用
runCountdown()

override func viewDidLoad() {
    super.viewDidLoad()
    func runCountdown() {
        Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
    }
    runCountdown()
    (print(countdown))
    (print(countdownLabel as Any))
}