I have the following code in the view controller:
- (void)viewDidLoad {
[super viewDidLoad];
ThemeManager *themer = [ThemeManager sharedInstance];
UIView *theView = self.view;
UIColor *forBackground = [themer backgroundColour];
[theView setBackgroundColor:forBackground];
}
but when execution gets to the setBackgroundColor line, I get the following error:
*** -[NSCFNumber CGColor]: unrecognized selector sent to instance 0x1237c40
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFNumber CGColor]: unrecognized selector sent to instance 0x1237c40'
There's got to be something simple that I'm doing wrong, how do I set the background colour?
Do I have to subclass the view and do it in there? I'd prefer not to have the extra class, even though that is better separation of the whole model/view/controller thing.
Update: value returned by [themer backgroundColour]
is constructed using colorWithPatternImage:
, could this make a difference?
Update: if I use a value in my ThemeManager that was constructed using colorWithRed:green:blue:alpha:, that it works OK. Is there any way to do this using a color with a background image? The following works OK:
[theView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"myimage.png"]]];
Update: this works OK too:
UIColor *forBackground = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myimage.png"]];
[theView setBackgroundColor:forBackground];
In my original example, the object returned from [themer backgroundColor]
was a UIColor
, so what's the problem?
When I step through with the debugger:
UIColor *forBackground = [themer backgroundColour];
results in forBackground
being of type NSConstantValueExpression *
and
UIColor *forBackground = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myimage.png"]];
results in forBackground
being of type UIDeviceRGBColor *
Here is the code for the ThemeManager's backgroundColour method:
- (UIColor *)backgroundColour {
if (backgroundColour == nil) {
backgroundColour = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myimage.png"]];
}
return backgroundColour;
}
backgroundColour is also the name of an instance variable.