문제

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