0

ユーザーがスペースを押した後、キーボードをUIKeyboardTypeNumbersAndPunctuationモードから通常のテキストモードに変更したい。

したがって、彼は「34ループ」のようなものを簡単に入力できます。

4

2 に答える 2

3

わかりました、直接入力通知を受け取ることはできませんが、考えられる解決策があります。

次のデリゲート メソッドを実装します。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

置換文字列には、その名前が示すように、テキスト フィールド内の既存の文字列を置換する文字列が含まれます。あなたができることは、この文字列の最後の文字をチェックし、それが実際にスペースである場合は、キーボードの種類を変更することです:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    char lastCharacter = [string characterAtIndex:[string length] - 1]; //Get the last input character in the ext field.
    if(lastCharacter == ' ')
    {
        //The last character is a space, so...
        textField.keyboardType = UIKeyboardTypeDefault;
        [textField resignFirstResponder];
        [textField becomeFirstResponder];
    }
    return YES;
}
于 2012-10-31T19:43:18.590 に答える
1

The answer given by Leonnears mostly answers the simple case, but there are many issues you need to consider. What happens if the user types 34, then a space, then delete? What happens when the user moves the caret from the end of loops to somewhere in the number part?

It all starts to get more difficult to cover each case. But more importantly, it starts to get annoying for the user as the keyboard starts changing on them. It makes it very difficult to just type in the text. I've never seen an app do what you propose and there is a good reason.

Let the user use the normal keyboard like they are used to. Everyone knows how to switch between letter and numbers. Having the keyboard automatically change will be unusual and confusing.

于 2012-10-31T20:10:53.860 に答える