我想找到一种方法来以编程方式触发选择器,使iPhone键盘从字母切换到数字。我知道我可以切换键盘类型,但我想知道是否有办法在不切换键盘类型的情况下执行此操作。
答案 0 :(得分:6)
您无法以编程方式将键盘(字母键盘与数字键盘切换到符号键盘),至少不能以任何公开支持的方式切换。如您所述,您可以更改第一响应者的文本输入特征的键盘类型或外观,但这与在不同的键盘之间切换不同,只有用户应该能够这样做。
答案 1 :(得分:6)
只是一种解决方法: 如果要在编辑UITextField时更改活动键盘的键盘(如从字母到数字键盘),请先将新值设置为文本字段的keyboardType属性,然后将resignFirstResponder,最后一条isFirstResponder消息发送到文本字段。 E.g。
textfield.keyboardType = UIKeyboardTypeNumberPad;
[textField resignFirstResponder];
[textField becomeFirstResponder];
夫特:
textField.keyboardType = .numberPad
textField.resignFirstResponder()
textField.becomeFirstResponder()
希望它有所帮助;-D
答案 2 :(得分: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;
}
检查我评论的地方“c完全照顾”
这将帮助您以编程方式切换键盘。
答案 3 :(得分: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];
}
}