我有 3 个带有文本的自定义 UIButton,当用户单击其中一个按钮时,我会检查答案是否正确。如果正确,我想将按钮背景设置为绿色,如果不是红色。然后我想在稍微延迟后自动显示下一个问题(他需要一点时间来看看之前的答案是否正确)。
问题是 UIbutton 背景在 IBAction 内部没有改变,但它在 setWordAndAnswers 函数内部改变。而且我不知道为什么会发生(((
这是代码:
-(void)setWordAndAnswers{
if([[currentCollection allWards] count]>0){
int randCard =arc4random_uniform([[currentCollection allWards] count]-1);
currentWord=[[currentCollection allWards]objectAtIndex:randCard];
tvMainWord.text=[currentWord originalWord];
int wrightAnser =arc4random_uniform(2);
for(int i=0;i[answers count];i++){
[[answers objectAtIndex:i]setBackgroundColor:[UIColor whiteColor]];
if(i==wrightAnser){
[[answers objectAtIndex:i]
setTitle:[currentWord wordTranslate] forState:UIControlStateNormal];
}
else{
[[answers objectAtIndex:i]
setTitle:[self getRandomAnswer] forState:UIControlStateNormal];
}
}
}
}
- (IBAction)onClickCheckAnswer:(id)sender {
if(!isGameRunning)
return;
UIButton *button = (UIButton *)sender;
if([[sender title]isEqual:[currentWord wordTranslate]]){
[button setBackgroundColor:[UIColor greenColor]];
}
else{
[button setBackgroundColor:[UIColor redColor]];
}
[button setNeedsDisplay];
[NSThread sleepForTimeInterval:0.3f];
[self setWordAndAnswers];
}