我有四个UIButtons
代表4个答案。我设法把所有东西都搞定了,用绿色显示正确答案,用红色显示错误答案。
我唯一的问题是,当用户选择了错误的答案时,我会将所有答案变灰。我想向用户显示正确的答案。我已经尝试了其他的陈述,但遗漏了一些东西,有什么建议吗?
-(IBAction)Answer1:(id)sender{
if (Answer1Correct == YES) {
[Answer1 setTitle:@"Correct" forState:UIControlStateNormal];
[Answer1 setBackgroundColor:[UIColor greenColor]];
[self RightAnswer];
}
else{
[Answer1 setBackgroundColor:[UIColor redColor]];
[Answer1 setTitle:@"Incorrect" forState:UIControlStateNormal];
[Answer2 setBackgroundColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0f]];
[Answer3 setBackgroundColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0f]];
[Answer4 setBackgroundColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0f]];
[self WrongAnswer];
}
}
答案 0 :(得分:1)
最好的方法是继承UIButton
。创建课程QuizQuestionButton
这个类将有方法
-setCorrectAnswer:(NSString *)correctAnswer
和-setRealAnswer:(NSString *)realAnswer
如果两者相同 - 将背景颜色设置为绿色,否则设置为红色。
-(void)setCorrectAnswer:(NSString *)correctAnswer {
_correctAnswer = correctAnswer;
}
-(void)setRealAnswer:(NSString *)realAnswer {
if (realAnswer == _correctAnswer) {
self.backgroundColor = [UIColor greenColor];
} else {
self.backgroundColor = [UIColor redColor];
}
}
还阅读有关设计模式 - 战略。这种模式可以帮助您避免if-else语句。
答案 1 :(得分:0)
试试这个:
if (Answer1Correct == YES) {
[Answer1 setTitle:@"Correct" forState:UIControlStateNormal];
[Answer1 setBackgroundColor:[UIColor greenColor]];
[self RightAnswer];
}
else{
[Answer1 setBackgroundColor:[UIColor redColor]];
[Answer2 setBackgroundColor:[UIColor greyColor]];
[Answer3 setBackgroundColor:[UIColor greyColor]];
[Answer4 setBackgroundColor:[UIColor greyColor]];
if(Answer2isCorrect)
{ [Answer2 setBackgroundColor:[UIColor greenColor]]; }
else if(Answer3isCorrect)
{ [Answer3 setBackgroundColor:[UIColor greenColor]]; }
else if(Answer4isCorrect)
{ [Answer4 setBackgroundColor:[UIColor greenColor]]; }
}
如果选择的答案有误,请先将所有其他按钮设置为灰色,然后找到正确的答案并将其更改为绿色。
您可能还希望在点击答案后停用所有按钮以防止进一步点击。