Search code examples
iosobjective-cuicolor

How to set [UIColor [variable here]]?


I have a .plist file, containing an array with dictionaries where each entry has a key-value-pair "color1:abc" and "color2:xyz". "abc" and "xyz" are always the standard iOS colors like "redColor" or "yellowColor" and so on.

My app has the "activeItem" from the .plist array and I want to set the color1 and color2 to the background of a view, so I tried something like:

self.view.backgroundColor = [UIColor [activeItem color2]];

(instead of: self.view.backgroundColor = [UIColor redColor];)

But that doesn't work... which syntax would be correct?


Solution

  • If [activeItem color2] returns a method name (redColor, yellowColor and so on), you can use performSelector:

    SEL selector = NSSelectorFromString([activeItem color2]); 
    self.view.backgroundColor = [UIColor performSelector:selector];