2016-07-24 34 views
1

我知道這個問題必須回答之前,但我仍然想知道爲什麼第一行代碼不能改變UIButton的標題,而第二行可以。除了第一行是從一個BigNerdRanch的Objective-C書中複製的......我將其作爲示例運行。通過titleLabel.text屬性更改UIButton的標題不起作用:爲什麼?

[email protected]"foo"; //doesn't work 

[self.assetTypeButton setTitle:@"foo" forState:UIControlStateNormal]; //this works and button title is changed. 
+0

[閱讀全文](http://stackoverflow.com/a/11417134/1066828)&[this](http://stackoverflow.com/a/4910521/1066828) –

+1

可能的[iOS: UIButton titleLabel - 它什麼都做?](http://stackoverflow.com/questions/4910446/ios-uibutton-titlelabel-does-it-do-anything-at-all) –

回答

0

Apple TitleLabeland setTitle

titleLabel

UIButton *button = [UIButton buttonWithType: UIButtonTypeSystem]; 
button.titleLabel.font   = [UIFont systemFontOfSize: 12]; 
button.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail; 

不要使用標籤對象設置文本顏色或陰影顏色的更好的解釋。 取而代之,使用setTitleColor:forState:和 setTitleShadowColor:forState:該類的方法使這些 發生更改。

的titleLabel屬性返回即使按鈕沒有 尚未顯示的值。系統 按鈕的屬性值爲零。

的setTitle

使用此方法來設置標題按鈕。 指定的標題從按鈕的關聯標籤 對象中派生出其格式。如果您爲 按鈕同時設置了標題和屬性標題,則該按鈕傾向於使用此 之間的屬性標題。

至少應設置正常狀態的值。如果某個狀態未指定 標題,則默認行爲是使用與UIControlStateNormal狀態關聯的標題 。如果沒有設置 UIControlStateNormal的值,則屬性默認爲 系統值。

相關問題