删除 UITextField 中的字母不起作用
Delete a letter in UITextField not working
我正在按照 使用逗号制作数字格式。问题是,每当输入 字母 时,textView
中的所有内容都会被删除。
[textField addTarget:self
action:@selector(formatNumberIfNeeded:)
forControlEvents:UIControlEventEditingChanged];
- (void)formatNumberIfNeeded:(UITextField *)textField{
NSString * currentTextWithoutCommas = [textField.text stringByReplacingOccurrencesOfString:@"," withString:@""];
NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber * numberFromString = [numberFormatter numberFromString:currentTextWithoutCommas];
NSString * formattedNumberString = [numberFormatter stringFromNumber:numberFromString];
textField.text = formattedNumberString;
}
我尝试在 shouldChangeTextInRange
中插入以下代码:
NSCharacterSet *numbersOnly = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
for (int i = 0; i < [textField.text length]; i++)
{
unichar c = [textField.text characterAtIndex:i];
if (![numbersOnly characterIsMember:c]) {
return NO;
}
}
那没有成功。所以我把它放在 formatNumberIfNeeded
中(在其他所有内容之前),而不是 return NO;
我插入:
textField.text = [textField.text stringByReplacingOccurrencesOfString:c withString:@""];
这给了我一个错误。不确定下一步该做什么。
错误:
ERROR Implicit conversion of 'unichar' (aka 'unsigned short') to 'NSString *' is disallowed with ARC
WARNING Incompatible integer to pointer conversion sending 'unichar (aka 'unsigned short') to parameter of type 'NSString *'
我已经尝试了您遵循的相同代码段,并且效果很好。查看以下片段和屏幕截图。
@property (weak, nonatomic) IBOutlet UITextField *txtField;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.txtField addTarget:self
action:@selector(formatNumberIfNeeded:)
forControlEvents:UIControlEventEditingChanged];
}
- (void)formatNumberIfNeeded:(UITextField *)textField{
// you'll need to strip the commas for the formatter to work properly
NSString * currentTextWithoutCommas = [textField.text stringByReplacingOccurrencesOfString:@"," withString:@""];
NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber * numberFromString = [numberFormatter numberFromString:currentTextWithoutCommas];
NSString * formattedNumberString = [numberFormatter stringFromNumber:numberFromString];
textField.text = formattedNumberString;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSRange illegalCharacterEntered = [string rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet]];
if ( illegalCharacterEntered.location != NSNotFound ) {
return NO;
}
return YES;
}
尝试为 UITextView
设置 keyboardType
属性 以防止输入字母。
[textField setKeyboardType:UIKeyboardTypeNumberPad];
从枚举列表中选择适合的键盘类型
typedef enum {
UIKeyboardTypeDefault, // Default type for the current input method.
UIKeyboardTypeASCIICapable, // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
UIKeyboardTypeNumbersAndPunctuation, // Numbers and assorted punctuation.
UIKeyboardTypeURL, // A type optimized for URL entry (shows . / .com prominently).
UIKeyboardTypeNumberPad, // A number pad (0-9). Suitable for PIN entry.
UIKeyboardTypePhonePad, // A phone pad (1-9, *, 0, #, with letters under the numbers).
UIKeyboardTypeNamePhonePad, // A type optimized for entering a person's name or phone number.
UIKeyboardTypeEmailAddress, // A type optimized for multiple email address entry (shows space @ . prominently).
UIKeyboardTypeDecimalPad, // A number pad including a decimal point
UIKeyboardTypeTwitter, // Optimized for entering Twitter messages (shows # and @)
UIKeyboardTypeWebSearch, // Optimized for URL and search term entry (shows space and .)
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated
} UIKeyboardType;
我正在按照 textView
中的所有内容都会被删除。
[textField addTarget:self
action:@selector(formatNumberIfNeeded:)
forControlEvents:UIControlEventEditingChanged];
- (void)formatNumberIfNeeded:(UITextField *)textField{
NSString * currentTextWithoutCommas = [textField.text stringByReplacingOccurrencesOfString:@"," withString:@""];
NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber * numberFromString = [numberFormatter numberFromString:currentTextWithoutCommas];
NSString * formattedNumberString = [numberFormatter stringFromNumber:numberFromString];
textField.text = formattedNumberString;
}
我尝试在 shouldChangeTextInRange
中插入以下代码:
NSCharacterSet *numbersOnly = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
for (int i = 0; i < [textField.text length]; i++)
{
unichar c = [textField.text characterAtIndex:i];
if (![numbersOnly characterIsMember:c]) {
return NO;
}
}
那没有成功。所以我把它放在 formatNumberIfNeeded
中(在其他所有内容之前),而不是 return NO;
我插入:
textField.text = [textField.text stringByReplacingOccurrencesOfString:c withString:@""];
这给了我一个错误。不确定下一步该做什么。
错误:
ERROR Implicit conversion of 'unichar' (aka 'unsigned short') to 'NSString *' is disallowed with ARC
WARNING Incompatible integer to pointer conversion sending 'unichar (aka 'unsigned short') to parameter of type 'NSString *'
我已经尝试了您遵循的相同代码段,并且效果很好。查看以下片段和屏幕截图。
@property (weak, nonatomic) IBOutlet UITextField *txtField;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.txtField addTarget:self
action:@selector(formatNumberIfNeeded:)
forControlEvents:UIControlEventEditingChanged];
}
- (void)formatNumberIfNeeded:(UITextField *)textField{
// you'll need to strip the commas for the formatter to work properly
NSString * currentTextWithoutCommas = [textField.text stringByReplacingOccurrencesOfString:@"," withString:@""];
NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber * numberFromString = [numberFormatter numberFromString:currentTextWithoutCommas];
NSString * formattedNumberString = [numberFormatter stringFromNumber:numberFromString];
textField.text = formattedNumberString;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSRange illegalCharacterEntered = [string rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet]];
if ( illegalCharacterEntered.location != NSNotFound ) {
return NO;
}
return YES;
}
尝试为 UITextView
设置 keyboardType
属性 以防止输入字母。
[textField setKeyboardType:UIKeyboardTypeNumberPad];
从枚举列表中选择适合的键盘类型
typedef enum {
UIKeyboardTypeDefault, // Default type for the current input method.
UIKeyboardTypeASCIICapable, // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
UIKeyboardTypeNumbersAndPunctuation, // Numbers and assorted punctuation.
UIKeyboardTypeURL, // A type optimized for URL entry (shows . / .com prominently).
UIKeyboardTypeNumberPad, // A number pad (0-9). Suitable for PIN entry.
UIKeyboardTypePhonePad, // A phone pad (1-9, *, 0, #, with letters under the numbers).
UIKeyboardTypeNamePhonePad, // A type optimized for entering a person's name or phone number.
UIKeyboardTypeEmailAddress, // A type optimized for multiple email address entry (shows space @ . prominently).
UIKeyboardTypeDecimalPad, // A number pad including a decimal point
UIKeyboardTypeTwitter, // Optimized for entering Twitter messages (shows # and @)
UIKeyboardTypeWebSearch, // Optimized for URL and search term entry (shows space and .)
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated
} UIKeyboardType;