繁体   English   中英

为什么我的标签没有显示我的计时器时间?

[英]why does my label doesn't show my timer time?

当我尝试在没有计时器的情况下做一个小计时器时,我的代码运行良好,但我的标签在运行时没有显示时间,这是怎么回事?

视图控制器.swift:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }




    @IBOutlet weak var out4: UILabel!
    @IBOutlet weak var out1: UIButton!
    @IBOutlet weak var out2: UITextField!
    @IBOutlet weak var out3: UILabel!

    @IBAction func click(_ sender: Any) {
        if out2.text != ""{
            var counterheure:Int = 23;
            var counterminute:Int = 60;
            var countersec:Int = 60;
            var counter:Int;
            counter = Int(out2.text!)!;
            counter = counter - 1;


            while counter != 0 {


                out3.text = "Days : \(counter); Hours : \(counterheure); Minutes : \(counterminute); Seconds : \(countersec); left before your next exam";
                countersec = countersec - 1;
                sleep(1);
                if countersec == 0{
                    counterminute = counterminute - 1;
                    countersec = 60;
                    if counterminute == 0{
                        counterheure = counterheure - 1;
                        counterminute = 60;
                        if counterheure == 0{
                            counter = counter - 1;
                            counterheure = 24;
                        }
                    }
                }


            }
        }
    }

}

因为您的循环阻塞了也负责更新 UI 的主线程。

我建议使用计时器:

let timer = Timer(timeInterval: 1, repeats: true) { (timer) in
    // This is called every second
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM