Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't set background colour for UIView from ViewController
    primarykey
    data
    text
    <p>I have the following code in the view controller:</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; ThemeManager *themer = [ThemeManager sharedInstance]; UIView *theView = self.view; UIColor *forBackground = [themer backgroundColour]; [theView setBackgroundColor:forBackground]; } </code></pre> <p>but when execution gets to the setBackgroundColor line, I get the following error:</p> <pre><code>*** -[NSCFNumber CGColor]: unrecognized selector sent to instance 0x1237c40 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFNumber CGColor]: unrecognized selector sent to instance 0x1237c40' </code></pre> <p>There's got to be something simple that I'm doing wrong, how do I set the background colour?</p> <p>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.</p> <p>Update: value returned by <code>[themer backgroundColour]</code> is constructed using <code>colorWithPatternImage:</code>, could this make a difference?</p> <p>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:</p> <pre><code>[theView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"myimage.png"]]]; </code></pre> <p>Update: this works OK too:</p> <pre><code>UIColor *forBackground = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myimage.png"]]; [theView setBackgroundColor:forBackground]; </code></pre> <p>In my original example, the object returned from <code>[themer backgroundColor]</code> was a <code>UIColor</code>, so what's the problem?</p> <p>When I step through with the debugger:</p> <pre><code>UIColor *forBackground = [themer backgroundColour]; </code></pre> <p>results in <code>forBackground</code> being of type <code>NSConstantValueExpression *</code></p> <p>and</p> <pre><code>UIColor *forBackground = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myimage.png"]]; </code></pre> <p>results in <code>forBackground</code> being of type <code>UIDeviceRGBColor *</code></p> <p>Here is the code for the ThemeManager's backgroundColour method:</p> <pre><code>- (UIColor *)backgroundColour { if (backgroundColour == nil) { backgroundColour = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myimage.png"]]; } return backgroundColour; } </code></pre> <p>backgroundColour is also the name of an instance variable.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload