输入时如何更改UITextField文本的值?
换句话说:
当我输入1时应显示1
当我键入10它应该显示10
当我输入100它应该显示100
但
当我键入1000,它应该显示1000
你能给任何想法吗?
解决方法
向文本字段控件添加“textFieldDidChange”通知方法.
@H_502_19@[textField addTarget:self
action:@selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
-(void)textFieldDidChange:(UITextField *)theTextField
{
NSLog(@"text changed: %@",theTextField.text);
NSString *textFieldText = [theTextField.text stringByReplacingOccurrencesOfString:@"," withString:@""];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *formattedOutput = [formatter stringFromNumber:[NSNumber numberWithInt:[textFieldText integerValue]]];
textField.text=formattedOutput;
}