Comparing two UIColors is not working in the first time

假装没事ソ 提交于 2019-12-13 07:09:43

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!