gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:59:27 25 4
gpt4 key购买 nike

我正在尝试根据我在另一个类中设置的属性来设置 View 的背景颜色。 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 * 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;
}
}

关于iphone - 从 UIColor 属性设置 UIView 的背景颜色时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7292246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com