我有一个简单的按钮,最初用表情符号贴上标签,我想做的就是一旦点击这个按钮就移除表情符号。
import UIKit
class ViewController: UIViewController {
@IBAction func touchCard(_ sender: UIButton) {
flipCard(withEmoji: "", on: sender)
}
func flipCard(withEmoji emoji: String, on button:UIButton){
if button.currentTitle == emoji {
button.setTitle("", for: UIControl.State.normal)
print("Removed emoji")
}
}
}当我逐步完成代码时,button.setTitle("", for: UIControl.State.normal)语句就会被执行,但是在单击按钮之后,表情符号不会消失,尽管它看起来已经褪色了。
编辑:标题确实会被更新,但是需要几(8-10)秒才能完成。用另一个表情符号代替表情符号几乎是瞬间的!是什么导致了这一切,我该如何解决呢?
PS:我正在跟着CS193P的讲座(第一课) 这里。
发布于 2019-05-08 17:22:25
您可能需要button.title(for: .normal)而不是button.currentTitle。
发布于 2019-05-08 17:30:59
如果按钮出现褪色,它可能被禁用。如果为禁用状态设置标题并执行button.setTitle("", for: UIControl.State.normal),则UIControl.State.disabled的标题不会发生任何变化。
检查button.setTitle("", for: UIControl.State.disabled)是否能做到这一点。
发布于 2019-05-09 03:53:06
您可以简单地将您的函数替换为下面的函数,这是我为您添加的。
func flipCard(withEmoji emoji: String, on button:UIButton){
if button.currentTitle == emoji {
button.setTitle("", for: .normal)
button.setTitle("", for: .selected)
button.setTitle("", for: .disabled)
print("Removed emoji")
}
}希望这样能对你有所帮助。
https://stackoverflow.com/questions/56046002
复制相似问题