簡體   English   中英

從UIColor屬性設置UIView的backgroundColor的問題

[英]Problem with setting UIView's backgroundColor from a UIColor property

我試圖根據我在另一個類中設置的屬性設置視圖的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的設置器:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM