【问题标题】:How to get date and time to show a clock in UILabel如何获取日期和时间以在 UILabel 中显示时钟
【发布时间】:2014-11-14 13:12:26
【问题描述】:

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

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

【问题讨论】:

    标签: swift uilabel


    【解决方案1】:

    使用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.0 秒的间隔更新。我不应该在运行应用程序时看到标签更新的时间间隔吗?
    • 对不起 - 错过了你的 CurrentDateAndTime 类只计算一次“当前”日期/时间作为常量;更新了我的答案以在每个tick 上执行此操作;您应该根据需要进行重构。
    • 小心调用 deinit { timer.invalidate() } 以防止内存泄漏。
    【解决方案2】:

    这是一个更新的 (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());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      相关资源
      最近更新 更多