在UIKeyboardTypeDecimalPad上响应DELETE

时间:2013-07-10 23:19:37

标签: ios keyboard decimal

这件事让我抓狂。有人如何在用户按下数字小键盘上的DELETE键的情况下回复用户?我找不到它的委托方法,我只在调用shouldChangeCharactersInRange()时看到一个空字符串。

谢谢!

1 个答案:

答案 0 :(得分:0)

我明白了......

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
                                                replacementString:(NSString *)string {

    if ([string length] == 0)
        return YES;

    NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    string = [[string componentsSeparatedByCharactersInSet:nonNumberSet] componentsJoinedByString:@""];
    textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];


return NO;

}