سؤال

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!

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

This works:

if ([[sender titleColorForState:UIControlStateNormal] isEqual:[UIColor redColor]])
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top