Reputation: 823
I have a simple button which is initially labelled with the emoji 🦁, and all that I'm trying to do is to remove the emoji once the button is clicked.
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")
}
}
}
As I step through the code, the statement button.setTitle("", for: UIControl.State.normal)
gets executed, however the emoji does not disappear although it does appear faded, after the button is clicked.
Edit: The title does get updated, but it takes a few (8-10) seconds to do so. Replacing the emoji with another emoji is almost instantaneous though! What could be causing this and how do I fix this?
PS: I'm following along the CS193P lecture (Lecture 1) here.
Upvotes: 0
Views: 131
Reputation: 494
You can simply replace your function like below function that i have added for you.
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")
}
}
Hope this way may help you.
Upvotes: 0
Reputation: 759
If the button appears faded, it might be disabled. If you set the title for the disabled state and execute button.setTitle("", for: UIControl.State.normal)
, nothing will happen to the title for UIControl.State.disabled
.
Check if button.setTitle("", for: UIControl.State.disabled)
does the trick.
Upvotes: 0
Reputation: 318814
You probably want button.title(for: .normal)
instead of button.currentTitle
.
Upvotes: 1