iOS: 当点击不同的按钮时改变按钮的背景颜色
iOS: Change background color of a button when different button is clicked
我的测验应用程序有 4 个答案,其中一个是正确的,它们都在一个集合视图中。当用户点击错误答案时,我需要通过更改正确答案的按钮颜色来向他们展示正确答案。我可以访问该按钮,但由于某种原因,按钮颜色没有改变。我错过了什么吗?
-(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];
}
}
调试器:好像有颜色
一个简单的解决方案是使用控制变量,当您想要显示正确的解决方案时,您可以更改它并重新加载集合视图。像
if(shouldShowAnswer){
cell.answerButton.backgroundColor = [UIColor greenColor]
}else{
cell.answerButton.backgroundColor = defaultColor
}
我的测验应用程序有 4 个答案,其中一个是正确的,它们都在一个集合视图中。当用户点击错误答案时,我需要通过更改正确答案的按钮颜色来向他们展示正确答案。我可以访问该按钮,但由于某种原因,按钮颜色没有改变。我错过了什么吗?
-(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];
}
}
调试器:好像有颜色
一个简单的解决方案是使用控制变量,当您想要显示正确的解决方案时,您可以更改它并重新加载集合视图。像
if(shouldShowAnswer){
cell.answerButton.backgroundColor = [UIColor greenColor]
}else{
cell.answerButton.backgroundColor = defaultColor
}