如何获得在UILabel中显示时钟的日期和时间

时间:2014-11-14 13:12:26

标签: swift uilabel

我是swift的新手(以及一般的编程),现在我正在开发一个项目,我想在标签中显示一个时钟。

现在我在我的模型中有这个:

class CurrentDateAndTime {

let currentTime = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .MediumStyle, timeStyle: .MediumStyle)
}

然后在我的视图控制器中使用此代码将其调用到标签中:

@IBOutlet weak var currentTimeLabel: UILabel!

let currentTime = CurrentDateAndTime()



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

    currentTimeLabel.text = currentTime.currentTime
}

问题是这不会在第二个间隔更新以显示实际时间。最终我需要在两个时间戳之间找到一个区别,以便我可以在一个"开始时间和#34;之间记录小时数。和#34;停止时间"。这甚至是最好/最简单的方法吗?任何有关我的目标的线索都将非常感激。

2 个答案:

答案 0 :(得分:12)

使用NSTimer以所需的时间间隔安排回调 - 在这种情况下,

  • 每秒tick()致电self
  • tick()获取当前时间字符串并更新UILabel

    class MyViewController {
        @IBOutlet weak var currentTimeLabel: UILabel!
    
        var timer = NSTimer()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view, typically from a nib.
    
            timer = NSTimer.scheduledTimerWithTimeInterval(1.0
                                                                target: self,
                                                              selector: #selector(tick),    
                                                              userInfo: nil, 
                                                               repeats: true)
        }
    
        @objc func tick() {
            currentTimeLabel.text = NSDateFormatter.localizedStringFromDate(NSDate(),
                                                                            dateStyle: .MediumStyle,
                                                                            timeStyle: .MediumStyle)
        }
    }
    

Swift 4.0更新:

class MyViewController : UIViewController {
    @IBOutlet weak var currentTimeLabel: UILabel!

    var timer = Timer()

    override func viewDidLoad() {
        super.viewDidLoad()
        timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector:#selector(self.tick) , userInfo: nil, repeats: true)
    }

    @objc func tick() {
        currentTimeLabel.text = DateFormatter.localizedString(from: Date(),
                                                              dateStyle: .medium,
                                                              timeStyle: .medium)
    }
}

答案 1 :(得分:6)

这是一个更新的(2016)Swift示例:

import Cocoa

class ViewController: NSViewController {
    @IBOutlet weak var labelClock: NSTextField!
    let dateFormatter = DateFormatter()

    override func viewDidLoad() {
       super.viewDidLoad()
       // Do any additional setup after loading the view.
       dateFormatter.dateStyle = .medium
       dateFormatter.timeStyle = .long
       Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateLabel), userInfo: nil, repeats:true);
    }

    override var representedObject: Any? {
       didSet {
           // Update the view, if already loaded.
       }
    }


    func updateLabel() -> Void {
        labelClock.stringValue = dateFormatter.string(from: Date());
    }
}