问题
When i click on OK button of an UIAlertView, i need to change the background color of the view, its default color is the white, so every time the user click on OK, i need to alternate between two colors, white and red for example:
-(void)changeColor{
if([self.view.backgroundColor isEqual: [UIColor whiteColor]]){
self.view.backgroundColor=[UIColor redColor];
}else {
self.view.backgroundColor=[UIColor whiteColor];
}
}
The problem is that on first OK click, the color is supposed to become red, however it doesn't get the red, so i need to click the OK button the second time to get the view background color with the red. am i missing something to get the color changed from the first time?
This is the alertView:didDismissWithButtonIndex
delegate method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex==0) {
//NSLog(@"It's Ok button which has been clicked");
//Do whatever you want, commonly call to another function like so:
[self changeColor];
}else
if (buttonIndex==1) {
//NSLog(@"It's Cancel button which has been clicked");
}
}
回答1:
It probably isn't the same "white" as returned by [UIColor whiteColor]
. If you pick the white color in IB for your view and log it, you get: UIDeviceRGBColorSpace 1 1 1 1
If you then set the color to [UIColor whiteColor]
, and log it again, you get: UIDeviceWhiteColorSpace 1 1
回答2:
As stated, it's not actually [UIColor whiteColor]. Set it manually when the view loads:
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor whiteColor];
}
来源:https://stackoverflow.com/questions/10942239/comparing-two-uicolors-is-not-working-in-the-first-time