Iphone 设置UIView'时出现问题;从UIColor属性中选择背景颜色
我试图根据在另一个类中设置的属性设置视图的背景色。view类部分如下所示:Iphone 设置UIView'时出现问题;从UIColor属性中选择背景颜色,iphone,objective-c,ios,core-data,uicolor,Iphone,Objective C,Ios,Core Data,Uicolor,我试图根据在另一个类中设置的属性设置视图的背景色。view类部分如下所示: // 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 * coverVi
// 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。
您可以直接设置背景色(无需附加属性):
或覆盖coverColor的设置程序:
-(void) setCoverColor:(UIColor*)color
{
if (coverColor != color)
{
[coverColor release];
coverColor = [color retain];
coverView.backgroundColor = coverColor;
}
}
您在哪里将颜色值传递给此处的coverColor属性?你真的得到了存储在tat中的值吗?chk使用NSLog…当我运行NSLog(@“%@”,noteblock.color)时;(存储在数据库中的颜色)我得到以下输出:UIDeviceGBColorSpace 1 0.831373 0.317647 1。这不起作用,因为作者想设置coverView
的-backgroundColor
,而不是单元格的颜色。您需要使用标记或其他iVar获取对coverView
的引用。Thins可以工作,但我不想设置视图的背景色,只想设置视图的一个子视图。对不起,我在问题中不清楚。这样:CGRect cover=CGRectMake(19.0,7.0,coverWidth,coverHeight);coverView=[[UIView alloc]initWithFrame:cover];coverView.layer.cornerRadius=5;coverView.backgroundColor=coverColor代码>我解决了这个问题,只对您的答案做了一点小小的修改,覆盖setCoverColor就成功了。谢谢
-(void) setCoverColor:(UIColor*)color
{
if (coverColor != color)
{
[coverColor release];
coverColor = [color retain];
coverView.backgroundColor = coverColor;
}
}