2011-08-27 45 views
0

調試器顯示某種顏色的值。請問我做錯了什麼此目標C代碼不會更改標籤文本的顏色

@synthesize textLabel; 
@synthesize textField; 
@synthesize sliderRed; 
@synthesize sliderGreen; 
@synthesize sliderBlue; 


- (void)dealloc 
{ 
    [textLabel release]; 
    [super dealloc]; 
} 

- (IBAction)updateLabel 
{ 
    NSString * textValue = [textField text]; 

    float red = [sliderRed value]/255.f; 
    float green = [sliderGreen value]/255.f; 
    float blue = [sliderBlue value]/255.f; 

    UIColor *textColour = [[UIColor alloc]initWithRed:red green:green blue:blue alpha:1.0]; 
    [textLabel setText:textValue]; 
    [textLabel setTextColor:textColour]; 
} 

回答

2

我猜sliderRed,sliderGreen和sliderBlue是UISlider實例嗎?他們的最小/最大值是多少?如果你離開它在默認情況下0.0到1.0那麼這段代碼會給你一些非常低值:

float red = [sliderRed value]/255.f; 
float green = [sliderGreen value]/255.f; 
float blue = [sliderBlue value]/255.f; 

您使用的UIColor的方法採用浮動參數從0.0到1.0這樣簡單地傳遞給它的滑塊值,而不將它們會工作。

並且不要忘記在該方法的末尾放置[textColour release];,否則每次調用該方法時都會泄漏一個新的UIColor實例。

+0

這就是它:)。在目標C的旁註中,當我認爲它不在範圍內時,我需要釋放上面創建的每個對象。 –

+0

對於每個alloc/copy/retain,你都需要一個版本。您可以使用自動回覆的對象。在這個例子中,你會用'[UIColor colorWithRed:red green:green blue:blue alpha:1.0]替換'[[UIColor alloc] initWithRed:red green:green blue:blue alpha:1.0];' –

1

你的代碼沒有錯。也許在其他地方出了問題。
嘗試更換這行:
UIColor *textColour = [[UIColor alloc]initWithRed:red green:green blue:blue alpha:1.0];

UIColor *textColour = [UIColor redColor];

,並檢查redColor工作。如果不是的話,那麼你的其他代碼可能會出錯。

1

更改圖紙屬性後,嘗試添加[textLabel setNeeedsDisplay]