正常,我经常通过以下方式设置背景颜色: aView.backgroundColor = [UIColor redColor];
现在我将所有颜色名称保存在NSArray中:
colorsArray = [[NSMutableArray alloc] initWithObjects:
@"blackColor",
@"darkGrayColor",
@"lightGrayColor",
@"whiteColor",
@"grayColor",
@"redColor",
@"greenColor",
@"blueColor",
@"cyanColor",
@"yellowColor",
@"magentaColor",
@"orangeColor",
@"purpleColor",
@"brownColor",
nil];
我想通过类似
的NSString指针设置背景NSInteger i = 0;
for (aView in viewArray) {
NSString *colorName = [colorsArray objectAtIndex:i];
aView.backgroundColor = color with colorName;//Can't find a method to set
i++;
}
我检查UIColor类,找不到任何用colorName指针设置颜色的方法。 请帮忙!还有别的办法吗?谢谢!
答案 0 :(得分:5)
一点点反思,药物就会消失......
NSInteger i = 0;
for (aView in viewArray) {
NSString *colorName = [colorsArray objectAtIndex:i];
aView.backgroundColor = [[UIColor class] performSelector:NSSelectorFromString(colorName)];
i++;
}
答案 1 :(得分:2)
aView.backgroundColor = [UIColor performSelector:NSSelectorFromString(colorName)];