10

我想找到一种方法来以编程方式触发导致 iPhone 键盘从字母切换到数字的选择器。我知道我可以切换键盘类型,但我想知道是否有办法在不切换键盘类型的情况下做到这一点。

4

4 回答 4

6

您不能以编程方式切换键平面(字母键平面到数字键平面到符号键平面),至少不能以任何公开支持的方式。正如您所提到的,您可以更改第一响应者的文本输入特征的键盘类型或外观,但这与在不同键盘之间切换不同,只有用户才能做到这一点。

于 2010-03-01T08:32:36.120 回答
6

只是一种解决方法:当您想在编辑 UITextField 时将活动键盘的键盘从字母更改为数字键盘时,首先将新值设置为文本字段的 keyboardType 属性,然后发送 resignFirstResponder,最后发送 becomeFirstResponder 消息文本域。例如

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

迅速:

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

希望它有所帮助;-D

于 2010-10-19T20:55:14.510 回答
1

看看这个:

- (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;

}

检查我在哪里评论“完全照顾”

这将帮助您以编程方式切换键盘。

于 2010-06-07T13:38:44.240 回答
1

对于自定义切换键盘类型,您可以使用键盘的附件视图

在此处输入图像描述

在此处输入图像描述

使键盘附件视图用于类型切换的代码......(希望剪下的代码是可以理解的)

// 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];
    }
 }
于 2018-05-17T14:01:24.870 回答