我有一个测验应用程序,它有 4 个答案,其中一个是正确的,它们都在一个 Collection View 中。当用户点击错误答案时,我需要通过更改正确答案的按钮颜色来向他们展示正确答案。我可以访问该按钮,但由于某种原因,按钮颜色没有改变。我错过了什么吗?
-(void)selectedAnswer:(UIButton *)sender{
NSLog(@"ANswer Selected : %ld",(long)sender.tag);
NSLog(@"CurrentAnswer : %@",_currentAnswer);
NSLog(@"Question : %@",[[GameManager manager] getCurrentQuestion]);
int correctAnswer = [[_currentAnswer stringByReplacingOccurrencesOfString:@" " withString:@""] intValue];
NSLog(@"Correct Answer Is : %d",correctAnswer);
if (correctAnswer == sender.tag){
NSLog(@"Correct Answer Index : %@",_currentAnswerIndex);
sender.backgroundColor = [UIColor greenColor];
//[self updateScore:true];
//[self nextQuestion];
}else{
NSLog(@"Correct Answer Index : %@",_currentAnswerIndex);
sender.backgroundColor = [UIColor redColor];
SignCollectionViewCell * cell = (SignCollectionViewCell *)[self collectionView:self._collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:[_currentAnswerIndex integerValue] inSection:0]];
NSLog(@"cell Tag %@",cell.answerButton);
cell.answerButton.backgroundColor = [UIColor greenColor];//NOT WORKING
// [self updateScore:false];
//[self nextQuestion];
}
}
调试器:好像有颜色
最佳答案
一个简单的解决方案是使用控制变量,当您想要显示正确的解决方案时,您可以更改它并重新加载 Collection View 。类似
if(shouldShowAnswer){
cell.answerButton.backgroundColor = [UIColor greenColor]
}else{
cell.answerButton.backgroundColor = defaultColor
}
关于iOS:单击不同按钮时更改按钮的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30383007/