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

Swift 如何获取在UILabel中显示时钟的日期和时间,swift,uilabel,Swift,Uilabel,我是swift新手(一般来说是编程),现在我正在做一个项目,我想在标签上显示一个时钟 现在,我的模型中有: class CurrentDateAndTime { let currentTime = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .MediumStyle, timeStyle: .MediumStyle) } 然后在my view controller中使用以下代码将其调用到标签中: @IBOutlet

我是swift新手(一般来说是编程),现在我正在做一个项目,我想在标签上显示一个时钟

现在,我的模型中有:

class CurrentDateAndTime {

let currentTime = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .MediumStyle, timeStyle: .MediumStyle)
}
然后在my view controller中使用以下代码将其调用到标签中:

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

问题是它不会在第二个时间间隔内更新以显示实际时间。最终,我将需要得到两个时间戳之间的差异,以便我可以记录“开始时间”和“停止时间”之间的小时数。这是最好/最简单的方法吗?如果有任何关于我目标的线索,我们将不胜感激。

使用
NSTimer
将回调安排在您想要的时间间隔内——在这种情况下

  • 每隔一秒钟在
    self
    上调用
    tick()
  • 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)
    }
}
这是一个更为流行的(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());
    }
}

我不确定我做错了什么,但标签仍然没有以1.0秒的间隔更新。当我运行应用程序时,难道我不能看到标签在间隔时间内更新吗?对不起,错过了您的
CurrentDateAndTime
类仅将“当前”日期/时间作为常量计算一次;更新我的答案,以便在每个
勾选
;您应该根据需要进行重构。只需小心并调用deinit{timer.invalidate()}以防止内存泄漏。