0

这件事快把我逼疯了。任何人如何回应用户按下数字键盘上的 DELETE 键?我找不到它的委托方法,并且在调用 shouldChangeCharactersInRange() 时只看到一个空字符串。

谢谢!

4

1 回答 1

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;

}

于 2013-07-10T23:29:49.707 回答