iphone

2012-04-13 66 views
0

如何在動態變化按鈕標題我有一個需要改變按鈕標題與現有標題 例如按鈕標題是「其他」,在運行時需要更改「其他(3)」。需要知道如何使用按鈕標題符合整數值(3)。iphone

請在此提供幫助 以下是按鈕源代碼。

mOthersBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [mOthersBtn setTitle:@"Others" forState:UIControlStateNormal]; 
     [mOthersBtn addTarget:self action:@selector(mOthersClick) forControlEvents:UIControlEventTouchUpInside]; 
     mOthersBtn.frame = CGRectMake(20, 320, 280, 25); 
     [self.view addSubview:mOthersBtn]; 

預先感謝..

+0

是否要整合該按鈕上的點擊次數?什麼是3? – Deviator 2012-04-13 07:46:58

+0

只是重新設置按鈕的標題? [mOthersBtn setTitle:@「Others(3)」forState:UIControlStateNormal]; – Novarg 2012-04-13 07:47:58

回答

3

只需使用stringWithFormat:

NSInteger count = 3; 
[button setTitle:[NSString stringWithFormat:@"Others (%i)", count] forState:UIControlStateNormal]; 
+0

(另外,請仔細閱讀字符串格式,它們非常有用) – EmilioPelaez 2012-04-13 07:47:35

-1

嘗試以下代碼

NSString *str = [NSString stringWithFormat:@"Run time text here"]; 


[button setTitle: str forState:UIControlStateNormal]; 
+0

@「此處運行時的文本」表示包含運行時文本的字符串。那裏你可以放置你的字符串。 NSString * str = [NSString stringWithFormat:@「%@」,StrRunTimeText]; – 2012-05-04 11:38:31

-1

NSInteger的C = 2; [button setTitle:[NSString stringWithFormat:@「Others(%i)」,c] forState:UIControlStateNormal];