2013-05-13 112 views
0

我有一個NSString像「S21」,並試圖將數字轉換爲按鈕的標記。爲什麼我不能更改我的UIButton上的標籤?

我做:

NSString *temp = [[array objectAtIndex:0] stringByReplacingOccurrencesOfString:@"s" withString:@""]; 
[_b0 setTag: [temp intValue]]; 

_B0是我的UIButton。

我運行的NSLog檢查標籤我跑setTag之後,但在按鈕的標籤並沒有改變,仍然爲0。

我在做什麼錯?

+1

記錄'temp'的值並檢查它是否正確。 – trojanfoe 2013-05-13 07:53:16

+1

NSLog記錄'temp'並查看它有什麼,還有'_b0'以確保它不是'nil'。在設置標籤後也可以設置NSLog(@「%d」,_ b0.tag)「。讓我們知道。 – samfisher 2013-05-13 07:54:04

+0

temp設置標籤前後顯示21,_b0顯示0。 – 2013-05-13 07:58:20

回答

1

您只需查看UIButtonIBOutlet連接,如果你在創建過程通過nib文件。

0
NSString *str = @"S21"; 
NSString *newStr = [str substringFromIndex:0]; 
0

這應該工作

NSString *temp = [[array objectAtIndex:0] stringByReplacingOccurrencesOfString:@"s" withString:@""]; 
_b0.tag = [temp intValue]; 
1

你的代碼是正確的,沒有問題,因爲我在我的最終實現。見下面我想你正在做的事情錯在其他代碼..

NSString *temp = [[array objectAtIndex:0] stringByReplacingOccurrencesOfString:@"s" withString:@""]; 

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
btn.frame = CGRectMake(20, 20, 100, 100); 
[self.view addSubview:btn]; 
[btn setTag: [temp intValue]]; 

NSLog(@"Tag %d",btn.tag); 

輸出:

標籤21

+0

其他的東西肯定是錯的。我的錯。對不起浪費大家時間。今天早些時候我復位了我的筆尖,並忘記將這個按鈕與_b0重新關聯。謝謝你們的幫助。 – 2013-05-13 08:06:06

+0

非常歡迎我親愛的,我希望你有你的答案。 – 2013-05-13 08:07:51

+0

@EdEdEddyChen檢查我的答案哥們.... – Rajneesh071 2013-05-13 08:10:00

0
*temp = [[array objectAtIndex:0] stringByReplacingOccurrencesOfString:@"s" withString:@""]; 
_b0.tag = [temp intValue]; 
相關問題