当我点击 UIAlertView 的 OK 按钮时,我需要更改视图的背景颜色,它的默认颜色是白色,所以每次用户点击 OK 时,我需要在两种颜色之间交替,例如白色和红色:
-(void)changeColor{
if([self.view.backgroundColor isEqual: [UIColor whiteColor]]){
self.view.backgroundColor=[UIColor redColor];
}else {
self.view.backgroundColor=[UIColor whiteColor];
}
}
问题是,在第一次单击确定时,颜色应该变成红色,但是它没有变成红色,所以我需要第二次单击确定按钮才能获得红色的视图背景颜色。我是否错过了从第一次开始改变颜色的东西?
这是alertView:didDismissWithButtonIndex
委托方法:
- (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");
}
}