views:

63

answers:

1

i have a global variable

UIColor *textColor;

I am update this variable by the code

textColor = [UIColor colorWithRed:fr green:fg blue:fb alpha:1.0];

then assigning this color to Label like this

myLabel.textColor = textColor;

It only work once, when i again call with updated values and assign label new values app crashes...

textColor = [UIColor colorWithRed:fr green:fg blue:fb alpha:1.0];
myLabel.textColor = textColor;
+2  A: 

First of all, you should almost never use global variables in Objective-C. They get very ugly as you get more code.

That being said, retain it after you create it to solve the crash, and release it before you assign something new to it. You're seeing the autorelease pool release the color for you since nothing owns it after your function exits.

cobbal
the textColor property of UILabel is marked with retain - this means the color will be retained by the myLabel.textColor assignment.Adding an extra retain to the textColor will not do anything useful.
diciu