UIButton文本内容不断重置每秒更新

我正在尝试更新一个带有测试值的按钮,并且我注意到每秒更新一次按钮标题文本会在几分之一秒内显示测试值,但随后会重置为Button的默认值。

这似乎是一个错误,但我想看看是否有一个更简单的解释。 在按下按钮之前,我试图等待10秒,但这似乎一直在发生。

任何想法如何使UIButton的功能如预期?

import UIKit

class ViewController: UIViewController {

    var testEntry = "its working"
    @IBOutlet weak var testButton: UIButton!
    @IBOutlet weak var testLabel: UILabel!

    @IBAction func runTest(sender:
        UIButton) {
        // The button value should equal the value of the label value, but every 2nd button press of the test button results in the title of the button value resetting to the default value
        dispatch_async(dispatch_get_main_queue()) {
            self.testLabel.text = "(self.testEntry)"
            self.testButton.titleLabel?.text = "(self.testEntry)"
        }
    }

这是github项目。


您不应该直接设置按钮标题标签的文本,您应该只将字体直接设置到标签上。 文本应该通过调用来设置

func setTitle(_ title: String?, forState state: UIControlState)

文本切换是因为您正在选择和取消选择该按钮,该按钮将在具有不同标题的某些状态之间切换。


您应该使用以下方法设置按钮标题文本...

self.testButton.setTitle(self.testEntry, forState: .Normal)

而不是titleLabel属性。

链接地址: http://www.djcxy.com/p/28225.html

上一篇: UIButton text content keeps resetting every second update

下一篇: line UIButtons