2013-06-20 25 views
0

因爲我是一個新手,我很困惑爲什麼這是不可能的。我創建了一個數組:NSMutableArray *labels並向其添加了幾個對象UILabel。我這樣做是因爲我想要輕易改變所有標籤的textcolour,而不必單獨做到這一點,但是當我打電話NSMutableArray標籤iphoneapp

_labels.textColor = [UIColor colorWithRed:137.0f/255.0f green:200.0f/255.0f blue:255.0f/255.0f alpha:1.0f];  

它給我,文字顏色不能用MutableArray使用的錯誤。有沒有另外一種方法呢?謝謝!

+0

你想改變陣列中所有標貼的顏色? – Durgaprasad

回答

1

在你目前的嘗試中,你會改變數組的textcolor(如果有的話)。您必須在陣列中的每個UILabel上調用該方法。 做在陣列中的所有項目的東西,你可以在蘋果的文檔中關於NSMutableArray使用enumerateObjectsUsingBlock:

[_labels enumerateObjectsUsingBlock:^(UILabel *label, NSUInteger idx, BOOL *stop){ 
    label.textColor = [UIColor colorWithRed:137.0f/255.0f green:200.0f/255.0f blue:255.0f/255.0f alpha:1.0f]; 
}]; 

,瞭解更多有關陣列的外觀:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

1

您需要遍歷數組中的項目,並調用每個標籤上的方法:

for (UILabel *label in _labels) { 
    label.textColor = [UIColor colorWithRed:137.0f/255.0f green:200.0f/255.0f blue:255.0f/255.0f alpha:1.0f];  
} 

數組不會爲你做的消息,除非你是問:

[_labels makeObjectsPerformSelector:@selector(setTextColor:) withObject:[UIColor colorWithRed:137.0f/255.0f green:200.0f/255.0f blue:255.0f/255.0f alpha:1.0f]];