2

I can see how to set the keyboard's overall type via:

self.myTextView.keyboardType = UIKeyboardTypeDefault;

How can I toggle the default keyboard's mode from ABC to 123 and back again via code? Basically the moment the user taps the @ character (the @ symbol is available when they're in 123 mode) I want to switch their keyboard back to ABC mode.

Any ideas?

enter image description here enter image description here

John Erck
  • 9,478
  • 8
  • 61
  • 71

1 Answers1

4

You might be able to accomplish this in by using the UITextViewDelegate. It allows you to intercept the keys as they are pressed in the UITextView. This will allow you to switch keyboards when the user presses a certain key. In order to revert back to the default state of the UIKeyboardTypeDefault keyboard, you'll need to change the keyboard type to another, then back to the default.

In your ViewController.h file, make it implement the UITextViewDelegate protocol:

@interface ViewController : UIViewController <UITextViewDelegate>

In your ViewController's viewDidLoad method in the ViewController.m, set the textField's delegate to the view controller:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.myTextView.delegate = self;
}

Finally, we need to capture the key as it is being entered and change the keyboard appropriately. We do this in the shouldChangeCharactersInRange: method.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if( [@"@" isEqualToString:text] )
    {
        NSLog( @"Toggling keyboard type");
        textView.inputView = nil;
        [textView resignFirstResponder];
        [textView setKeyboardType:UIKeyboardTypeEmailAddress];
        [textView becomeFirstResponder];
        [textView reloadInputViews];

        textView.inputView = nil;
        [textView resignFirstResponder];
        [textView setKeyboardType:UIKeyboardTypeDefault];
        [textView becomeFirstResponder];
        [textView reloadInputViews];

    }
    return YES;
}

So I was originally confused and thought you actually wanted to change the keyboard type. Now that I'm reading your comment, I realize that you're actually wanting to reset the keyboard to it's default state of showing the letters, instead of numbers and special characters. The above code now does that.

wottle
  • 13,095
  • 4
  • 27
  • 68
  • Using this approach I can't get the keyboard to switch back to ABC. When I step through the code my textView's type is always `UIKeyboardTypeDefault` and that's what I want. I would like to switch the "mode" within the default keyboard to toggle the user away from the numbers and punctuation options (i.e. where the "@" is) to ABC which is where the letters are. I'll update the post to show screenshots. Any ideas? – John Erck Aug 06 '14 at 03:15
  • It definitely seems like that should work but it's not causing the keyboard to switch modes. Have you tested this approach? The only difference I can think of between what you have here and my code is that I'm working with a textView rather than a textField but the methods should still apply... – John Erck Aug 06 '14 at 13:51
  • I did, I have it running in the simulator and it works fine. Every time I hit the @ key, I get reverted back to the default keyboard in the default state (with the letter keys). If you set a breakpoint in the `shouldChangeCharactersInRange:` method, are you hitting the breakpoint? If not, the delegate has not been assigned correctly. If it is hitting it, can you see that the keyboard type is changed after the first call to `setKeyboardType` and before the second? – wottle Aug 06 '14 at 19:15
  • I just tested in the simulator rather than on device and it's not switching for me there either. When I run a debug session and step through the code with the following line added `NSLog(@"Keyboard type = %ld", textView.keyboardType);` I see that at first it's "Keyboard type = 0", then switches to "Keyboard type = 7" (UIKeyboardTypeEmailAddress), then switches back to "Keyboard type = 0" but none of this graphically changes the state of the keyboard for the user. What could be going wrong? – John Erck Aug 06 '14 at 20:29
  • 1
    OK, I've updated it for use with a UITextView. It seems like UITextView requires slightly different code to update the keyboard type. Let me know if it's working for you, because I tested it in a demo project here and it works as I believe you need. If it does work, please accept the answer so that others know this is how to do it. Thanks! – wottle Aug 07 '14 at 22:52