2010-01-15 48 views
5

我在表格上输入UITextFields来输入值。 其中一些字段只接受数字。我使用键盘类型UIKeyboardTypeNumbersAndPunctuationshouldChangeCharactersInRange来过滤字符。如何在触摸空格键时防止键盘从数字改为字母?

此外,所有的修正将被禁用:

textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; 
textField.autocorrectionType = UITextAutocorrectionTypeNo; 
textField.autocapitalizationType = UITextAutocapitalizationTypeNone; 

上的数字仅领域,当空格键被触摸,键盘变为字母。 我知道这是默认行为。 我想忽略空格键,不想更改键盘类型。

有什么方法可以改变这种默认行为吗?

PS:其他数字键盘类型不是一个选项。我需要标点符号!

谢谢

回答

4

我不认为有可能修改键盘的行为。

但是,你可以实现从UITextFieldDelegate协议拦截的空间(和撇号)这样的textField:shouldChangeCharactersInRange:replacementString:,它似乎工作:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 
    if ([string isEqualToString:@" "] || [string isEqualToString:@"'"]) { 
     NSMutableString *updatedString = [NSMutableString stringWithString:textField.text]; 
     [updatedString insertString:string atIndex:range.location]; 
     textField.text = updatedString; 
     return NO; 
    } else { 
     return YES; 
    } 
} 
相关问题