我在场景编辑器中创建了标签,我试图通过单击按钮来更新它,但每次单击按钮时,我都会在 timeLabel 行“ fatal error :在展开可选文件时意外发现 nil” 时收到此错误值(value) (lldb)“
这是我正在使用的代码,它应该有一个值,所以我不确定我做错了什么
@IBAction func stopButton(_ sender: Any) {
endTime = DispatchTime.now()
print ("\(endTime)")
timerStarted = false
let start = startTime.uptimeNanoseconds
let end = endTime.uptimeNanoseconds
let nanoTime = end - start
let timeInterva = Double(nanoTime) / 1_000_000_000
let total = Double(timeInterva) * hourRate
print("\(timeInterva)")
timerLabel.text = String(total)
}
最佳答案
您的@IBOutlet 到timerLabel 可能未连接。转到 Storyboard -> ViewController -> 单击场景层次结构中的 ViewController -> 左上角(参见屏幕截图)timerLabel 所在的连接检查器,并确保您看到如下所示的内容:
关于ios - 更新 UILabel 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42215867/