ios - 设置和删除UIButton文本标签

标签 ios swift uibutton

我正在尝试实现tik tak toe游戏。因此,我有9个按钮,每当我按下它们时,它们都会将其文本标签安装为“X”或“O”

sender.setTitle("X", for: .normal)
// or
sender.setTitle("O", for: .normal)

但是,当游戏结束时,我想删除所有文本标签并遇到问题-我无法删除文本标签。我尝试了几种变体,但仍然无法理解问题。我试过了 :
button.setTitle(nil, for: .normal)
button.setTitle("", for: .normal)
button.titleLabel?.text = ""
button.titleLabel?.text = nil

没用即使我的“重置失败”后在此按钮上看不到文本,仍会设置文本。
即使我正在做所有变体来删除文本,然后调用
button.titleLabel?.text

我得到的不是空行或零,我得到的是“X”! (如果之前有“x”文本)

最佳答案

我检查了...所有这些线都工作..问题在于您的连接

@IBOutlet weak var button: UIButton!
    override func viewDidLoad() {

        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            self.button.setTitle("", for: .normal)
            //self.button.backgroundColor = .red


        }
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

enter image description here

关于ios - 设置和删除UIButton文本标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61532966/

相关文章:

objective-c - 在 UIView 层上找不到 '-setBorderColor:' 方法?

iphone - 如何在 iPhone 中使用 userAcceleration

swift - 如何以相同的方法使用不同的按钮来获得不同的结果?

ios - 在 Swift 中使用 if let 语法解开双可选(Type?)?

ios - IPA 文件困惑。如何确定我的 .ipa 文件是否可以在另一台 iPhone 上运行?

ios - 如何在 Swift 中使用 YouTube Api v3 正确检查 UIImage 是否为零?

swift - 如何在 swift 单元测试中验证方法是否在具体类中被调用

swift - 在 UIAlert 中按下 Ok 时如何调用函数

ios - 按钮按下功能不起作用/按钮不会按下 Objective C

iphone - 下拉列表实现