Reputation: 733
Because I'm a newbie I'm confused about why this is not possible. I have created an array: NSMutableArray *labels
and added a few UILabel
objects to it. I did this because I wanted to easily change the textcolour of all of the labels without having to do it individually, but when I call
_labels.textColor = [UIColor colorWithRed:137.0f/255.0f green:200.0f/255.0f blue:255.0f/255.0f alpha:1.0f];
It gives me an error that textColor can't be used with a MutableArray. Is there another way of doing this? Thanks!
Upvotes: 0
Views: 49
Reputation: 2999
In your current try you would change the textcolor of the array (if there where one). You have to call the method on every UILabel
in the array.
to do something on all items in an array you can use 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];
}];
for more information about array look at the apple documentation about NSMutableArray
: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html
Upvotes: 1
Reputation: 119031
You need to iterate over the items in the array and call the method on each label:
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];
}
The array won't do the messaging for you unless you ask it to:
[_labels makeObjectsPerformSelector:@selector(setTextColor:) withObject:[UIColor colorWithRed:137.0f/255.0f green:200.0f/255.0f blue:255.0f/255.0f alpha:1.0f]];
Upvotes: 1