christophriepe
christophriepe

Reputation: 1673

Why is UIButton.titleLabel?.text always switching back after I changed it in Swift 5 (UIKit)?

I ran into an issue with my UIButton.

After the User presses the button, it activates an IBAction which is supposed to change the titleLabel.text of my UIButton.

self.btnAppleHealthKit.titleLabel?.text = "Already pressed."

Whenever I press the button, the text changes for about a quarter second to "Already pressed", but switches back immediately to the previous text.

Important Note: The whole thing is inside a completion handler but I wrapped it into DispatchQueue.main.async { }

Can you guys think of an idea why this error appears? Thanks for your help in advance.

Upvotes: 2

Views: 863

Answers (1)

Sateesh Yemireddi
Sateesh Yemireddi

Reputation: 4399

It seems, UIButton does not allow to configure text or color of titleLabel.

From apple doc.s

Do not use the label object to set the text color or the shadow color. Instead, use the setTitleColor(:for:) and setTitleShadowColor(:for:) methods of this class to make those changes. To set the actual text of the label, use setTitle(_:for:) (button.titleLabel.text does not let you set the text).

Upvotes: 4

Related Questions