10

I want to find a way to programmatically fire the selector that causes the iPhone keyboard to switch from letters to numbers. I am aware that I can switch the keyboard type, but I want to know if there is a way to do this without switching the keyboard type.

1
  • is this for uiwebview or some other subclass of uiview? EDIT: Sorry, just noticed the uitextfield tag Feb 28, 2010 at 0:24

5 Answers 5

6

You cannot switch keyplanes (alphabetic keyplane to numeric keyplane to symbol keyplane) programatically, at least not in any way that is publicly supported. As you mentioned, you can change the keyboard type or appearance of the text input traits of the first responder, but this is different than switching between the different keyplanes, which only the user should be able to do.

6

Just a workaround: When you want to change the keyplane of an active keyboard like from alphabet to number pad while editing an UITextField, first set the new value to the keyboardType property of the textfield, next send a resignFirstResponder, finally a becomeFirstResponder message to the textfield. E.g.

textfield.keyboardType = UIKeyboardTypeNumberPad;
[textField resignFirstResponder];
[textField becomeFirstResponder];

Swift:

textField.keyboardType = .numberPad
textField.resignFirstResponder()
textField.becomeFirstResponder()

Hope it helps ;-D

3
  • Any word on apps getting rejected for doing this? Aug 6, 2011 at 21:36
  • 2
    Use textField.reloadInputViews() instead!
    – Nike Kov
    Dec 19, 2016 at 5:12
  • @NikKov textField.reloadInputViews() does not work by itself.
    – David
    Aug 10, 2017 at 17:52
2

For custom switching keyboard types you can use accessoryView of Keyboard

enter image description here

enter image description here

Code to make the keyboard accessoryView for type switching... (hopefully the snipped code is understandable)

// You can call this in viewDidLoad (initialization of the ABC/Done view) 
- (void)setAccessoryViewForKeyboard{
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 44)];
    UIBarButtonItem *changeKeyboard = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(switchKeyboardTypes)];
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *choose = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", @"") style:UIBarButtonItemStylePlain target:_textField action:@selector(resignFirstResponder)];
    [toolbar setItems:@[changeKeyboard, space, choose]];
    [self.textField setInputAccessoryView:toolbar];

}

// changing the left button text ('ABC' and '123')
- (void)setTitleForSwitchingKeyboardButton{
    NSString *firstButtonText = self.textField.keyboardType == UIKeyboardTypeDefault ? NSLocalizedString(@"123", @"") : NSLocalizedString(@"ABC", @"");
    [[[(UIToolbar *)self.textField.inputAccessoryView items] firstObject] setTitle:firstButtonText];
}


- (void)switchKeyboardTypes{
    if (self.textField.keyboardType == UIKeyboardTypeDefault){
        [self setTextFieldKeyboardType:UIKeyboardTypeNumberPad];
    } else {
        [self setTextFieldKeyboardType:UIKeyboardTypeDefault];
    }
}

- (void)setTextFieldKeyboardType:UIKeyboardTypeNumberPad:(UIKeyboardType)keyboardType {

    [self.textField setKeyboardType:keyboardType];

    if ([_textField isFirstResponder]) {

        _changingKeyboardType = YES;
        [self.textField resignFirstResponder];
        [self.textField becomeFirstResponder];
        _changingKeyboardType = NO;
    }

    [self setTitleForSwitchingKeyboardButton];
}


-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (!_changingKeyboardType) {
        // you can set the default keyboard type here:
        // [self setTextFieldKeyboardType:UIKeyboardTypeNumberPad];
        // [self setTextFieldKeyboardType:UIKeyboardTypeDefault];
        [self setTitleForSwitchingKeyboardButton];
    }
 }
1

Have a look at this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil)
    {
        cell = [ [ [UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0f];
        cell.textLabel.textColor = [UIColor whiteColor];
    }

    UITextField * textField = [ [ UITextField alloc] initWithFrame:CGRectMake(55.0f, 10.0f, 230.0f, 31.0f)];
    textField.textColor = [UIColor whiteColor];

    textField.delegate = self;
c this care fully   ///////////if(indexPath.row == 0)
c this care fully   //  textField.keyboardType = UIKeyboardTypeDefault;
c this care fully   //else
c this care fully   //  textField.keyboardType = UIKeyboardTypePhonePad;
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    [cell.contentView addSubview:textField];

    if(indexPath.row == 0)
    {
        self.sender = textField;
        cell.textLabel.text = @"From:";
    }
    else 
    {
        self.mobileNumber = textField; 
        cell.textLabel.text = @"To:";
    }
    [textField release];
    if(indexPath.row == 1)
    {
        UIButton * contactsButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
        [contactsButton addTarget:self action:@selector(addContact:) forControlEvents:UIControlEventTouchUpInside];
        cell.accessoryView = contactsButton;

    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}

Check where I commented "c this care fully"

This will help you to switch keyboards programmatically.

0
0

Maybe

keyboardType = .numbersAndPunctuation 

will help. It starts the keyboard in numeric mode and you can switch back to alphabet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.