从 UIColor属性设置 UIView 的背景颜色时出现问题



我试图根据我在另一个类中设置的属性设置视图的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];

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

cell.coverColor = noteblock.color;行改变了属性coverColor,但没有改变coverView的backgroundColor。你可以直接设置背景颜色(不需要附加属性):

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

或重写coverColor:

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

最新更新