setBackgroundColor on Custom View

8 views
Skip to first unread message

Scott Kyle

unread,
Jan 6, 2010, 7:52:26 PM1/6/10
to objec...@googlegroups.com
Hi,

I'm very new to Cappuccino, and so I'm not sure if this is a bug or not. I have a custom view that calls setBackgroundColor on itself in its initWithFrame.  When adding that as a subview in applicationDidFinishLaunching it works properly, but when adding it with Interface Builder everything works except that the background color is never set.

I hope that made sense.

Thanks!

Francisco Tolmasky

unread,
Jan 6, 2010, 10:43:02 PM1/6/10
to Cappuccino & Objective-J
Hi Scott,

The reason for this is that the view is archiving its color (which is
nil), then setting it after your initWithFrame:. Now, I'm sure what I
just said doesn't make any sense, because Interface Builder doesn't
allow you to change the background color (so why is it archiving it?).
The reason is that Atlas does, which is why its being encoded and
decoded. If you were in Atlas, it would make sense that if you set the
color to "red" in the inspector you wouldn't want it to change
afterwards just because you are setting the default color in
initWithFrame:. There is a very simple workaround for this:

- (void)awakeFromCib
{
[self setBackgroundColor:whatever];
}

awakeFromCib is for things that should happen *after* the properties
that are set by the user.

Scott Kyle

unread,
Jan 7, 2010, 2:48:32 PM1/7/10
to Cappuccino & Objective-J
Thanks for reply Francisco, it makes sense. Theoretically, if I wanted
to create a plugin for instance, how would I programmatically provide
the default bacgkroundColor that could then be overridden by Atlas?
Would I just check for nil in awakeFromCib?

Francisco Tolmasky

unread,
Jan 7, 2010, 3:12:47 PM1/7/10
to Cappuccino & Objective-J
In the case that this was a plugin for Atlas, then the initWithFrame:
you originally used would "just work". This is because that view would
be archived (with your color choice), then unarchived by Atlas in the
plugin with that same color choice. The reason it currently doesn't
work is because there is a discrepancy between the color you want and
the color Atlas wants, which would not be the case if you provide the
view.

Francisco Tolmasky

unread,
Jan 7, 2010, 3:13:54 PM1/7/10
to Cappuccino & Objective-J
To better visualize this, imagine you were setting the default size in
initWithFrame:. It wouldn't work in Atlas because the user has resized
the view. However, if you provided this view at that default size in
the plugin, then it would work as long as the user doesn't resize it.

Scott Kyle

unread,
Jan 7, 2010, 6:14:46 PM1/7/10
to Cappuccino & Objective-J
Thanks a lot, that clears it up for me.
Reply all
Reply to author
Forward
0 new messages