ios - 更新 UILabel 不起作用

标签 ios swift uilabel

我在场景编辑器中创建了标签,我试图通过单击按钮来更新它,但每次单击按钮时,我都会在 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 所在的连接检查器,并确保您看到如下所示的内容:

而不是这样的: enter image description here

关于ios - 更新 UILabel 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42215867/

相关文章:

ios - UITableView 在 iPad 上没有填满整个屏幕

ios - 我们可以从后台线程更新UI吗?

ios - UILabel 具有背景 View + 遵循文本布局的填充

ios - 在 Swift 3 中动态调整 UILabel 的宽度

ios - 使用 Objective-C 在 iOS 的 NSMutableDictionary 中添加值

arrays - 快速检索 JSON 数据

ios - 获取文档目录收件箱文件并移动

ios - 水平堆栈 View 内的标签宽度相等?

ios - 当 View 出现时以编程方式定义 UILabel 的大小

iphone - 将多个上下文与核心数据一起使用时出现错误 133000