0

我有一个 IBAction,当有人在字段中输入文本时调用它。然后我验证输入。如果我确定有错误,我会显示一条消息并希望用户再次输入相同的字段。我不想让他们选择文本字段来调出键盘(效果很好),我只想让键盘显示。

我正在做[SymbolEntered becomeFirstResponder];IBAction 中的最后一条语句,但键盘仍然消失了。我把它放错地方了吗?任何帮助,将不胜感激。谢谢。

- (IBAction)textFieldDoneEditing:(id)sender {

    DebugMsg.text = nil;
    DebugMsg2.text = nil;
    DebugMsg3.text = nil;

    NSLog (@"done editing");
    NSLog (@"%@", SymbolEntered.text);

    if ([SymbolEntered.text isEqualToString:nil])
    {
        Result.textColor = [UIColor redColor];
        Result.text = @"You must enter a symbol!";
        [SymbolEntered becomeFirstResponder];
    }
    else 
    {
        if ([SymbolEntered.text isEqualToString: 
             [NSString stringWithCString:elements_table2[el_tbl_idx-1].element_symbol]])
        {
            correct_count++;
            Result.textColor = [UIColor greenColor];
            Result.text = @"Correct!";
            Score.hidden = FALSE;
            Score.text = [NSString stringWithFormat:@"Score: %d out of %d - %d Percent", correct_count, el_count+1,
                                        (correct_count*100)/(el_count+1)];
            GetNextElementButton.hidden = FALSE;
            SymbolEntered.enabled = FALSE;
            el_count++;
            attempts = max_tries + 1;
        } 
        else 
        {
            Score.hidden = TRUE;
            Result.textColor = [UIColor redColor];
            if (attempts < max_tries)
            {
                if (attempts+1 == max_tries)
                {
                    Result.text = @"Sorry, one more try -";
                }
                else
                {    
                    Result.text = @"Sorry, try again - ";
                }
                GetNextElementButton.hidden = TRUE;
                attempts++;
            }
            else
            {    
                Result.text = [[NSString alloc] initWithFormat: @"Sorry.  The correct answer is %@", 
                   [NSString stringWithCString:elements_table2[el_tbl_idx-1].element_symbol]];
                Score.hidden = FALSE;
                Score.text = [NSString stringWithFormat:@"Score: %d out of %d - %d Percent", correct_count, el_count+1,                      (correct_count*100)/(el_count+1)];
                GetNextElementButton.hidden = FALSE;
                SymbolEntered.enabled = FALSE;
                el_count++;
            }
        }
    }   

    [SymbolEntered becomeFirstResponder];        

    NSLog (@"end of textfieldoneediting");
}  
4

3 回答 3

2

您可以在 UITextField textFieldShouldEndEditing: 委托方法中进行验证。如果您从该回调中返回 NO,则文本字段将保持为第一响应者,并且键盘不会消失。(当然,如果还没有,您必须将控制器对象设为文本字段的委托。)

于 2012-04-25T04:21:12.417 回答
0

稍后尝试调用 [textField becomeFirstResponder]?还要确保 UITextField 指针不是 nil 或其他东西。如需更多帮助,请显示您的一些代码,很难说出问题出在哪里。

于 2012-04-24T23:15:07.267 回答
0
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
   [textField resignFirstResponder];
   return YES;
}

试试这个它会退出键盘。

于 2012-07-17T08:27:57.217 回答