0

我有 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];

}

4

3 回答 3

1

Make sure the UIButton is of type custom like this:

UIButton *myButton = [UIButton buttonWithType: UIButtonTypeCustom];

Additionally why are you calling NSThread sleep?, if you want to delay execution of setwordandanswers I would use performSelector:afterDelay, or if you have a dependency then use NSOperationQueue addDependecy.

Hope that helps.

于 2013-08-02T12:14:22.630 回答
0

我从您给定的代码中获取了以下几行:

UIButton *button = (UIButton *)sender;
if([[sender title]isEqual:[currentWord wordTranslate]]){

将其更改为以下内容:

UIButton *button = (UIButton *)sender;
if([[button titleForState:UIControlStateNormal] isEqual:[currentWord wordTranslate]]){

然后,我相信它应该工作。

于 2013-08-02T12:25:52.300 回答
0

尝试使用发件人本身,而不是在您的方法中创建一个新按钮..并将发件人的类型更改为(UIButton *)(仅在它不适用于(id)时更改)

- (IBAction)onClickCheckAnswer:(UIButton *)sender

  if([[sender title]isEqual:[currentWord wordTranslate]]){
    [sender setBackgroundColor:[UIColor greenColor]];
}

    else{
        [sender setBackgroundColor:[UIColor redColor]];
    }

:)

请给个反馈..

于 2013-08-02T12:20:51.617 回答