我是否可以通过编程方式启动;切换到数字键盘“;iPhone键盘上的按钮
我想找到一种方法,以编程方式启动选择器,使iPhone键盘从字母切换到数字。我知道我可以切换键盘类型,但我想知道是否有一种方法可以在不切换键盘类型的情况下进行切换。您不能通过编程方式切换键盘平面(字母键盘平面到数字键盘平面到符号键盘平面),至少不能以任何公开支持的方式进行切换。正如您所提到的,您可以更改第一响应者的文本输入特征的键盘类型或外观,但这与在不同的键盘平面之间切换不同,只有用户才能这样做。看看这个:我是否可以通过编程方式启动;切换到数字键盘“;iPhone键盘上的按钮,iphone,uitextfield,uikeyboard,Iphone,Uitextfield,Uikeyboard,我想找到一种方法,以编程方式启动选择器,使iPhone键盘从字母切换到数字。我知道我可以切换键盘类型,但我想知道是否有一种方法可以在不切换键盘类型的情况下进行切换。您不能通过编程方式切换键盘平面(字母键盘平面到数字键盘平面到符号键盘平面),至少不能以任何公开支持的方式进行切换。正如您所提到的,您可以更改第一响应者的文本输入特征的键盘类型或外观,但这与在不同的键盘平面之间切换不同,只有用户才能这样做。看看这个: - (UITableViewCell *)tableView:(UITableView
- (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;
}
检查我在哪里评论“小心”
这将帮助您以编程方式切换键盘。这只是一个解决方法:
当您想在编辑UITextField时将活动键盘(如字母表)的键盘平面更改为数字键盘时,首先将新值设置为textfield的keyboardType属性,然后发送resignFirstResponder,最后向textfield发送becomeFirstResponder消息。例如
textfield.keyboardType = UIKeyboardTypeNumberPad;
[textField resignFirstResponder];
[textField becomeFirstResponder];
斯威夫特:
textField.keyboardType = .numberPad
textField.resignFirstResponder()
textField.becomeFirstResponder()
希望有帮助-D对于自定义切换键盘类型,您可以使用键盘的附件视图 使键盘附件视图用于类型切换的代码。。。(希望剪下的代码可以理解)
这是用于uiwebview还是uiview的其他子类?编辑:抱歉,刚刚注意到uitextfield标记。应用程序上的任何word都会因为这样做而被拒绝?请改用
textField.reloadInputViews()
@NikKovtextField.reloadInputViews()
本身不起作用。
// 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];
}
}