当我点击 UIAlertView 的 OK 按钮时,我需要改变 View 的背景颜色,它的默认颜色是白色,所以每次用户点击 OK 时,我需要在两种颜色之间切换,白色和例如红色:
-(void)changeColor{
if([self.view.backgroundColor isEqual: [UIColor whiteColor]]){
self.view.backgroundColor=[UIColor redColor];
}else {
self.view.backgroundColor=[UIColor whiteColor];
}
}
问题是在第一次单击“确定”时,颜色应该变成红色,但它并没有变成红色,所以我需要第二次单击“确定”按钮才能使 View 背景颜色变为红色。我是不是错过了第一次改变颜色的东西?
这是 alertView:didDismissWithButtonIndex
委托(delegate)方法:
- (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");
}
}
最佳答案
它可能与 [UIColor whiteColor]
返回的“白色”不同。如果您在 IB 中为您的 View 选择白色并记录它,您将得到:UIDeviceRGBColorSpace 1 1 1 1
如果您随后将颜色设置为 [UIColor whiteColor]
,并再次记录,您将得到:UIDeviceWhiteColorSpace 1 1
关于ios - 第一次比较两个 UIColors 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10942239/