2012-10-28 56 views
0

好吧,所以我試圖給7個不同的標籤名稱使用數組和循環。給數組的標籤名稱

代碼:

id huller[] = {hul18.text, hul17.text, hul16.text, hul15.text, hul14.text, hul13.text, hul12.text, hul11.text, hul10.text, hul9.text, hul8.text, hul7.text, hul6.text, hul5.text, hul4.text, hul3.text, hul2.text, hul1.text}; 

for (int i = 0; 7 > i; i++) { 
    huller[i] = [NSString stringWithFormat:@"%i", x + 1]; 
    NSLog(@"%@", huller[i]); 
} 

在NSLog的名稱的改變,但他們沒有在模擬器改變。哪裏不對?

回答

0

假設hul18,hul17等都是的UILabel對象,那麼這樣做:

NSArray *labels = [ hul18, hul17, hul16, hul15, hul14, hul13, hul12, hul11, hul10, hul9, hul8, hul7, hul6, hul5, hul4, hul3, hul2, hul1 ]; 

// Change the text of every label in the array 
for (int i = 0; i < labels.count; i++) { 
    UILabel *label = labels[i]; 
    label.text = [NSString stringWithFormat:@"%i", x + 1]; // Do you really want 'x' here or 'i'? 
    NSLog(@"%@", label.text); 
} 
+0

這種方法給了我這樣的警告: - [__ NSCFString的setText:]:無法識別的選擇發送到實例0x8a1d4b0 (但是,我確實想要x而不是我:)) – user1411094

+0

這意味着'標籤'數組具有NSString對象而不是UILabel對象。您的「標籤」數組是否與我發佈的代碼相符? hul18和其他UILabel對象? – rmaddy

+0

我改變了它,現在我得到了這個錯誤 - [UILabel objectAtIndexedSubscript:]:無法識別的選擇器發送到實例0x74b9e70 :( – user1411094

0

如果您希望文本也改變,那麼您必須手動設置文本。

NSArray *labels = //Array of labels; 
for (int i = 0; 7 > i; i++) { 
    huller[i] = [NSString stringWithFormat:@"%i", x + 1]; 
    labels[i].text = huller[i]; 
    NSLog(@"%@", huller[i]); 
}