删除UITextField中的字母不起作用

时间:2015-04-19 06:13:02

标签: ios objective-c uitextfield nsnumberformatter

我跟随this使用逗号制作数字格式。问题是,每当输入字母时,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 *'

2 个答案:

答案 0 :(得分:0)

我尝试了你所遵循的相同代码片段并且工作正常。请查看下面的摘要和屏幕截图。

@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;
}

enter image description here

enter image description here

答案 1 :(得分:0)

尝试设置keyboardType的{​​{1}}属性,以防止输入信件。

UITextView

从枚举列表中选择适合的键盘类型

 [textField setKeyboardType:UIKeyboardTypeNumberPad];