Reputation: 37
It is not a problem but I found out that UIButton.setTitle
does not set UIButton.titleLabel.text
to ""
. So if you want to set UIButton.titleLabel.text
to ""
you should use UIButton.titleLabel.text = ""
after UIButton.setTitle("", for: .normal)
.
In playground it looks like:
import UIKit
let button = UIButton()
button.setTitle("1", for: .normal)
print(button.titleLabel?.text ?? "no value")
//print 1
button.setTitle("2", for: .normal)
print(button.titleLabel?.text ?? "no value")
//print 2
button.setTitle("3", for: .normal)
print(button.titleLabel?.text ?? "no value")
//print 3
button.setTitle("", for: .normal)
print(button.titleLabel?.text ?? "no value")
//print 3 not ""
button.titleLabel?.text = ""
print(button.titleLabel?.text ?? "no value")
//print ""
But of course button.titleLabel?.text = ""
works only after button.setTitle("", for: .normal)
button.setTitle("3", for: .normal)
button.titleLabel?.text = ""
print(button.titleLabel?.text ?? "no value")
//print 3
I do not understand why this works this way. Perhaps someone can explain why?
Upvotes: 1
Views: 599
Reputation: 5823
You can see following apple description regarding button title set. See the red marked rectangle.
Sender is instance of UIButton
As per apple documentation:
To set the actual text of the label, use setTitle(_:for:).
button.titleLabel.text does not let you set the text.
I hope this will help you.
Upvotes: 3