在 IBAction 中禁用的 UIButton 重新启用自身

Posted

技术标签:

【中文标题】在 IBAction 中禁用的 UIButton 重新启用自身【英文标题】:UIButton disabled in IBAction re-enables itself 【发布时间】:2014-10-16 20:57:46 【问题描述】:

我有一个 UIButton,它链接到 UIView 中的 IBAction。在此 IBAction 中,我更改了 UIButton 的 titleLabel 的文本并禁用了 UIButton。但是,我发现当动作触发时,按钮会在重新启用之前禁用自身并在瞬间更改其文本。这几乎就像它在按下按钮时发生,然后它会自行撤消。有人可以帮忙吗?如果有用的话,我正在使用最新的 Xcode 和 ios 8 SDK。

//this is the IBAction...it happens, but the enabled property and text change quickly undoes itself.
@IBAction func solvePuzzle(sender: AnyObject) 
    self.activity.startAnimating()
    self.solveButton.enabled = false
    self.solveButton.titleLabel?.text = "Solving..."

【问题讨论】:

【参考方案1】:

按钮的文本不应该这样设置。我对 swift 不是很熟悉,但是正确的方法应该是这样的:

self.solveButton.setTitle("Solving...", forState:UIControlState.Normal);

还要确保在 touchUpInside 操作上调用该方法。

【讨论】:

你是对的!这确实使改变坚持下去!我很困惑为什么另一种方法有效但自己解开了,但这似乎已经成功了!

以上是关于在 IBAction 中禁用的 UIButton 重新启用自身的主要内容,如果未能解决你的问题,请参考以下文章

如何在nib文件中按下时禁用UIButton高亮显示?

如何禁用 UIButton 突出显示操作

用户单击时如何禁用 UIButton 的突出显示

UIButton 子类@IBAction 不起作用

如何在不重叠的情况下在 UIButton 中实现两个 IBAction?

在主视图控制器的自定义单元格中使用来自 UIButton 的 IBAction