The color object that you create with colorWithRed:green:blue:alpha:
is autoreleased, so by the time buttonTapped:
is called, the object has already been deallocated, so you have a pointer that points to garbage data (a dangling pointer).
You could either switch to using ARC (automatic reference counting), and/or create a retained property for myColor
. That would look like this in your header:
@property (nonatomic, retain) UIColor *myColor;
Then, instead of setting the instance variable directly, use:
self.myColor = [UIColor colorWithRed:0.41 green:0.107 blue:0.252 alpha:1.0];
in viewDidLoad
. If you don't use ARC, don't forget to release the color in dealloc
:
- (void)dealloc
{
[_myColor release];
[super dealloc];
}