此目标C代码不会更改标签文本的颜色
问题描述:
调试器显示某种颜色的值。请问我做错了什么此目标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];
}
答
我猜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实例。
答
你的代码没有错。也许在其他地方出了问题。
尝试更换这行:UIColor *textColour = [[UIColor alloc]initWithRed:red green:green blue:blue alpha:1.0];
到UIColor *textColour = [UIColor redColor];
,并检查redColor工作。如果不是的话,那么你的其他代码可能会出错。
答
更改图纸属性后,尝试添加[textLabel setNeeedsDisplay]
。
这就是它:)。在目标C的旁注中,当我认为它不在范围内时,我需要释放上面创建的每个对象。 –
对于每个alloc/copy/retain,你都需要一个版本。您可以使用自动回复的对象。在这个例子中,你会用'[UIColor colorWithRed:red green:green blue:blue alpha:1.0]替换'[[UIColor alloc] initWithRed:red green:green blue:blue alpha:1.0];' –