Anders
Anders

Reputation: 2941

Problem with setting UIView's backgroundColor from a UIColor property

I'm trying to set a view's backgroundColor based on a property that I set in another class. The view class partly looks like this:

// Interface iVar and property
UIColor * coverColor;
@property (nonatomic, retain) UIColor * coverColor;

// Where I set up the view
CGRect cover = CGRectMake(19.0, 7.0, coverWidth, coverHeight);
UIView * coverView = [[UIView alloc] initWithFrame:cover];
coverView.layer.cornerRadius = 5;
coverView.backgroundColor = coverColor;
[self.contentView addSubview:coverView];
[coverView release];
coverView = nil;

// In my other class where I try to set the color
cell.coverColor = noteblock.color;

// noteblock is a instance of a custom model (NSManagedObject) class. It have a property called color. The type is set to Transformable. It looks like this:
@property (nonatomic, retain) UIColor * color;
@dynamic color;

// I set the color like this when I create new Noteblock objects:
newNoteblock.color = [[[UIColor alloc] initWithRed:255.0/255.0 green:212.0/255.0 blue:81.0/255.0 alpha:1] autorelease];

When I run the app in the simulator, no color shows, it's transparent. Any ideas on how to solve this?

Upvotes: 0

Views: 846

Answers (1)

Felix
Felix

Reputation: 35384

The line cell.coverColor = noteblock.color; changes the property coverColor but not the backgroundColor of coverView. You could set the background color directly (without additional property):

coverView = [cell coverView];
coverView.backgroundColor = noteblock.color;

or override the setter of coverColor:

-(void) setCoverColor:(UIColor*)color
{
    if (coverColor != color)
    {
        [coverColor release];
        coverColor = [color retain];
        coverView.backgroundColor = coverColor;
    }
}

Upvotes: 1

Related Questions