iOS:单击不同按钮时更改按钮的背景颜色

时间:2015-05-21 19:46:09

标签: ios objective-c uibutton

我有测验应用程序有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];
    }
}

调试器:颜色似乎在那里

enter image description here

1 个答案:

答案 0 :(得分:2)

一个简单的解决方案是使用控制变量,当您想要显示正确的解决方案时,您需要更改它并重新加载集合视图。

之类的东西
if(shouldShowAnswer){
  cell.answerButton.backgroundColor = [UIColor greenColor]
}else{
  cell.answerButton.backgroundColor = defaultColor
}