0

I need to compare a color name. I have this code but it doesn't work =(

What should I do?

-(IBAction)buttonSelect:(id)sender {
  if ([sender color] == [UIColor redColor]) {
    // do something.
  }
}

Thanks!

Souljacker
  • 774
  • 1
  • 13
  • 35
Fabio
  • 1,913
  • 5
  • 29
  • 53
  • 1
    Also, read up on pointers and pointer comparisons. If you previously programmed in Java or C#, for instance, `==` does not do what you think it does in Objective-C. – dandan78 Mar 03 '14 at 16:27
  • Do you want to compare the `UIColor` objects or the name of the colors? – Souljacker Mar 03 '14 at 16:35

2 Answers2

4

Try this:

if ([[sender titleColorForState:UIControlStateNormal] isEqual:[UIColor redColor]]) 
{
   // do something
}

This is how you compare objects in objective-c, as objects are pointers and you can't compare pointers with ==. I'd advise you to look into this

Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
1

This works:

if ([[sender titleColorForState:UIControlStateNormal] isEqual:[UIColor redColor]])
Bucket
  • 7,415
  • 9
  • 35
  • 45
Fabio
  • 1,913
  • 5
  • 29
  • 53