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 に答える