問題描述
所以我試圖在單擊 UIButton 時更新它的文本.我正在使用以下行來更改文本:
So I'm trying to update the text on a UIButton when I click it. I'm using the following line to change the text:
calibrationButton.titleLabel.text = @"Calibration";
我已驗證文本正在更改,但是當我運行應用程序并單擊按鈕時,它會在一瞬間更改為校準",然后立即恢復為默認值.任何想法為什么會發生這種情況?我需要調用某種刷新函數嗎?
I have verified that the text is changing, but when I run the app and I click on the button, it changes to "Calibration" for a split second and then goes right back to its default value. Any ideas why this might be happening? Is there some sort of refresh function I need to be calling?
推薦答案
在布局子視圖時,UIButton 會使用自己的標題值設置其 titleLabel 的文本值,這樣您就可以為這四個設置最多四個不同的字符串狀態(正常、突出顯示、選中、禁用).
When laying out its subviews, a UIButton will set its titleLabel's text value using its own title values, so that you can set up to four different strings for the four states (normal, highlighted, selected, disabled).
因為這個特性,直接設置titleLabel的文本不會持久化,會在布局子視圖時被按鈕重置.
Because of this feature, setting the titleLabel's text directly won't persist, and will be reset by the button when it lays out its subviews.
這是更改按鈕狀態的標題文本所必須執行的操作.
This is what you have to do to change the title text for a button's state.
[calibrationButton setTitle:@"Calibration" forState:UIControlStateNormal];
這篇關于更改 UIButton 文本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!