Domanda

I'm trying to change the UIKeyboardType to the alphabet keyboard when the user types a space, mirroring the effect of typing an apostrophe. However, my code won't change the keyboard appearance until the user dismisses the keyboard and then brings it back again.

Edit: To clarify, the keyboard type starts as UIKeyboardTypeNumbersAndPunctuation and I want to change to ASCIICapable, because the typical user input is in the form of "# cups flour". I realized that the ASCIICapable keyboard has this functionality built-in, so presenting the ASCII capable keyboard but showing the numbers/punctuation first would work.

Here's my code:

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

    if ([string isEqualToString:@" "]) {
        textField.keyboardType = UIKeyboardTypeASCIICapable;
    }

return YES;
}
È stato utile?

Soluzione

dismiss the keyboard and then become the first responder again. In my code, I created a IBOutlet for the textfield *tf. It worked.

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

    if ([string isEqualToString:@" "]) {
        self.tf.keyboardType = UIKeyboardTypeNumberPad;
        [textField resignFirstResponder];
        [self.tf becomeFirstResponder];
    }

    return YES;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top