我想限制在UITextField中输入的数字。这是我的代码
#define MAX_NO_OF_DIGITS_BEFORE_DECIMAL 8
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
switch (textField.tag)
{
case BUY_ITEM_TEXTFIELD_TAG:
{
if (![textField.text length]>0) {
textField.text = [CURRENCY_TYPE stringByAppendingFormat:@"%@",self.buyTextField.text];
}
}
break;
case BID_INCREMENT_TEXTFIELD_TAG:
{
if (![textField.text length]>0) {
textField.text = [CURRENCY_TYPE stringByAppendingFormat:@"%@",self.bidTextField.text];
}
}
break;
case STARTING_BID_TEXTFIELD_TAG:
{
if (![textField.text length]>0) {
textField.text = [CURRENCY_TYPE stringByAppendingFormat:@"%@",self.startingBidTextField.text];
}
}
break;
case SHIPPING_CHARGES_TEXTFILED:
{
if (![textField.text length]>0) {
textField.text = [CURRENCY_TYPE stringByAppendingFormat:@"%@",self.chargesTextField.text];
}
}
break;
default:
break;
}
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
// Check if we have 8 digits before decimal points
NSString *tempStr = [newString stringByReplacingOccurrencesOfString:CURRENCY_TYPE withString:@""];
if ([tempStr length]==MAX_NO_OF_DIGITS_BEFORE_DECIMAL)
{
// Check if we have only 1 decimal point
NSArray *arrayOfString = [newString componentsSeparatedByString:@"."];
if ([arrayOfString count] > 2 )
{
return NO;
}
else {
textField.text = [NSString stringWithFormat:@"%@%@%@",CURRENCY_TYPE,tempStr,@"."];
}
}
// Check if we have only 1 decimal point
NSArray *arrayOfString = [newString componentsSeparatedByString:@"."];
if ([arrayOfString count] > 2 )
{
return NO;
}
// Check if we have only 2 digits after decimal point
if([arrayOfString count]>=2)
{
NSString *sepStr=[NSString stringWithFormat:@"%@",[arrayOfString objectAtIndex:1]];
return !([sepStr length]>2);
}
return YES;
}
但我的问题是当我输入12345678时,它会在8&之后放置小数点。再次放置8,所以我的输出看起来像12345678.8
而不是12345678.
我想要实现的是
1)用户只能在十进制和&之前输入MAX_NO_OF_DIGITS_BEFORE_DECIMAL。小数后2位数
我无法理解为什么最后8个会在此重复。有人能帮助我吗?
提前致谢。
答案 0 :(得分:3)
尝试以下代码
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
// Check if we have 3 digits before decimal points
int maxNoOfDigitsBeforeDecimal = 3;
// Check if we have 2 digits after decimal points
int maxNoOfDigitsAfterDecimal = 2;
// Check if we have no decimal point yet and more than 3 digits are entered
NSArray *arrayOfString = [newString componentsSeparatedByString:@"."];
if (([arrayOfString count] == 1 ) && ([newString length] > maxNoOfDigitsBeforeDecimal))
{
return NO;
}
// Check if we have only 1 decimal point
if ([arrayOfString count] > 2 )
return NO;
// Check if we have only 2 digits after decimal point
if([arrayOfString count] >= maxNoOfDigitsAfterDecimal)
{
NSString *sepStr = [NSString stringWithFormat:@"%@",[arrayOfString objectAtIndex:1]];
return !([sepStr length] > maxNoOfDigitsAfterDecimal);
}
return YES;
}
答案 1 :(得分:1)
首先,将记录添加到每个步骤以查看发生的情况或调试。
问题是你正在修改字符串(以创建newString变量)。然后,您将句点添加到结尾并将其设置为文本字段的新值。然后你返回yes(后面的检查不使用新的文本字段值)。所以你添加'8'和句点,然后要求框架添加'8'。