简体   繁体   English

快速更新UILabel与dispatch_async不起作用

[英]Swift update UILabel with dispatch_async not working

Why doesn't this work? 为什么不起作用?

dispatch_async(dispatch_get_main_queue(), {
    self.timeStringLabel.text = "\(self.timeStringSelected)"

    println(self.timeStringLabel.text)
})

I'm trying to update a label in Swift but the UI for the label never changes. 我正在尝试在Swift中更新标签,但标签的UI从未更改。 I keep googling it, but I can't find any responses that don't use dispatch_async. 我一直在搜索它,但找不到任何不使用dispatch_async的响应。 What am I doing wrong? 我究竟做错了什么?

1st Edit: I was mistaken. 1st编辑:我错了。 I'm not printing the updated text. 我没有打印更新的文本。 The text never changes. 文字永远不会改变。 It always prints out Optional("0") if that helps. 如果有帮助,它将始终打印出Optional(“ 0”)。 The default value is 0 as defined in the Storyboard. 情节提要中定义的默认值为0。

I have tried it with and without dispatch_async without any success. 我已经尝试过和没有dispatch_async都没有成功。 I also tried adding self.timeStringLabel.setNeedsDisplay() Immediately after updating the text, but that also doesn't work. 我还尝试在更新文本后立即添加self.timeStringLabel.setNeedsDisplay(),但这也不起作用。

Edit 2: Here's the complete function + UILabel declaration 编辑2:这是完整的功能+ UILabel声明

@IBOutlet weak var timeNumberLabel: UILabel!

@IBAction func timeNumberButtonPressed(sender: UIButton) {

    println("Number Selected. Tag \(sender.tag)")

    dispatch_async(dispatch_get_main_queue()) {

        self.timeNumberOneButton.selected = false
        self.timeNumberTwoButton.selected = false
        self.timeNumberThreeButton.selected = false
        self.timeNumberFourButton.selected = false

        if sender.tag == 0{
            self.timeNumberSelected = 0
        } else if sender.tag == 1 {
            self.timeNumberSelected == 5
        } else if sender.tag == 2 {
            self.timeNumberSelected == 10
        } else {
            self.timeNumberSelected == 24
        }

        sender.selected = true

        self.timeNumberLabel.text = "\(self.timeNumberSelected)"

        self.timeNumberLabel.setNeedsDisplay()


        println(self.timeNumberLabel.text)
    }
}

The label is clearly visible as shown in this picture. 如图所示,标签清晰可见。 I didn't think it would be this hard to implement, but I was very wrong. 我不认为实施起来会很困难,但是我错了。 I'm willing to bet it's something really simple that I'm missing. 我敢打赌,这是我真正缺少的东西。

在此处输入图片说明

Try adding the line 尝试添加线

self.timeStringLabel.setNeedsDisplay()

(after the label change) (更改标签后)

Because the code is run asynchronously, the interface-updating methods may miss the change and not display it (especially if a time-consuming bit of code occurs before the label change). 因为代码是异步运行的,所以接口更新方法可能会忽略更改而不会显示更改(特别是如果在更改标签之前花了一些时间的代码)。 Adding this code forces the interface to check for and display any changes it may have missed. 添加此代码将强制接口检查并显示其可能遗漏的任何更改。

This should be used after any asynchronous task that changes the interface, as the task's running may overlap with the interface methods, resulting in a missed change. 应在任何更改接口的异步任务之后使用此方法,因为该任务的运行可能与接口方法重叠,从而导致丢失更改。

*Thanks to the iOS Development Journal *感谢iOS开发杂志

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM