Domanda

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!

È stato utile?

Soluzione

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

Altri suggerimenti

This works:

if ([[sender titleColorForState:UIControlStateNormal] isEqual:[UIColor redColor]])
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top