从UIColor属性设置UIView的backgroundColor时出现问题

时间:2011-09-03 09:18:41

标签: iphone objective-c ios core-data uicolor

我正在尝试根据我在另一个类中设置的属性设置视图的backgroundColor。视图类部分看起来像这样:

// 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];

当我在模拟器中运行应用程序时,没有颜色显示,它是透明的。关于如何解决这个问题的任何想法?

1 个答案:

答案 0 :(得分:1)

cell.coverColor = noteblock.color;更改属性coverColor,但不更改coverView的backgroundColor。 您可以直接设置背景颜色(没有其他属性):

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

或覆盖coverColor的设置者:

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