UIButton 使用 titlelabel.text 以及 setTitle 的差別

大家好

煩請大家幫我看一下一個 UIButton 的例子 :


/* some code in .h ... */
@interface SecindVC : UIViewController
{
IBOutlet UIButton *xyz ;
NSString *str1 ;
}

@property(retain) IBOutlet UIButton *xyz ;
-(IBAction)btnpressed:(id)sender ;


/*以下是 .m 檔 */
/* ... some code in .m file ... */

-(IBAction)btnpressed:(id)sender
{
//xyz.titleLabel.text = str1 ;
[xyz setTitle:str1 forState:UIControlStateNormal];
NSLog(@"in btnpressed:") ;
NSLog(str1);

}

我的以上簡單例子中, str1 已經使用 [[NSString alloic] initwith...] 產生物件並給過初值了 ,
好奇的是 , xyz 這個 button , 當被 按下去時,我使用 xyz.titlelabel.text = str1 ;
無法改變 xyz button 的文字, 但是 [xyz setTitle..] 這個方式可以 !!

請問 這兩個方法差別在那 ?...為什麼 titleLabel.text runtime 修改行不通 ?

barfatchen wrote:
大家好煩請大家幫我看...(恕刪)

UiButtom Class Reference中有做解釋
titleLabel
A view that displays the value of the currentTitle property for a button. (read-only)

所以你不能使用titleLabel給予參數
只能使用[UiButtom setTitle]來設定
謝謝小朱..原來是 readonly !!
文章分享
評分
評分
複製連結

今日熱門文章 網友點擊推薦!