2011-09-03 76 views
0

我想根据我在另一个类中设置的属性来设置视图的backgroundColor。视图类部分看起来像这样:从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]; 

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

+0

你在哪里传递颜色值来这里coverColor财产?你真的获得了存储在tat中的价值吗? chk使用NSLog ... – booleanBoy

+0

当我运行NSLog(@“%@”,noteblock.color); (存储在数据库中的颜色)我得到以下输出:UIDeviceRGBColorSpace 1 0.831373 0.317647 1. – Anders

回答

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; 
    } 
} 
+0

这不起作用,因为作者想要设置'coverView'的'-backgroundColor'而不是单元的'-backgroundColor'。您需要使用标签或其他iVar来引用'coverView'。 –

+0

Thins有效,但我不想设置视图的backgroundColor,而是视图的子视图之一。对不起,在这个问题上并不清楚。像这样:'CGRect cover = CGRectMake(19.0,7.0,coverWidth,coverHeight); coverView = [[UIView alloc] initWithFrame:cover]; coverView.layer.cornerRadius = 5; coverView.backgroundColor = coverColor;' – Anders

+0

我解决了它,只是对答案进行了非常轻微的修改,覆盖了setCoverColor的技巧。谢谢! – Anders