0

正常的,我经常通过以下方式设置背景颜色: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 的指针设置颜色的方法。请帮忙!还有什么办法吗?谢谢!

4

2 回答 2

5

稍微反思一下,药就下去了……

NSInteger i = 0;
for (aView in viewArray) {
    NSString *colorName = [colorsArray objectAtIndex:i];
    aView.backgroundColor = [[UIColor class] performSelector:NSSelectorFromString(colorName)];
   i++;
}
于 2013-02-22T15:07:56.353 回答
2
aView.backgroundColor = [UIColor performSelector:NSSelectorFromString(colorName)];
于 2013-02-22T15:08:58.737 回答