ObjC - 使用NSRange,允许用户只输入一个小数点 -

时间:2011-04-21 14:07:35

标签: iphone objective-c

在这里构建一个简单的计算器,让我的脚在iOS开发中运行。我想禁止输入多个小数点。把我的逻辑放在评论中。

它构建并运行没有任何问题,但它不会阻止多个“。”的输入。任何帮助表示赞赏。感谢。

- (IBAction)digitPressed:(UIButton *)sender
{
    NSString *digit = [[sender titleLabel] text];
    if (userIsInTheMiddleofTypingANumber) {
        [display setText:[[display text] stringByAppendingString:digit]];
        // if the user enters a decimal point, check if the existing display text contains a "." if not, then allow it, but if it does, do not allow it.
        NSString *decimal = @".";
        NSRange range = [digit rangeOfString:decimal];
            if (range.location == NSNotFound) {
                [display setText:digit];
            }

    } else {
        [display setText:digit];
        userIsInTheMiddleofTypingANumber = YES;
    }
}

5 个答案:

答案 0 :(得分:2)

- (IBAction)digitPressed:(UIButton *)sender
{
    NSString *digit = [[sender titleLabel] text];
    NSString *decimal = @".";
    BOOL decimalAlreadyEntered = [display.text rangeOfString:decimal].location == NSNotFound ? NO : YES;

    if (userIsInTheMiddleofTypingANumber) {
        if (([digit isEqual:decimal] && !decimalAlreadyEntered) || !([digit isEqual:decimal])) {
            [display setText:[[display text] stringByAppendingString:digit]];
        }
    }
    else if (display.text isEqual:@"0" && digit == decimal){
         [display setText:[[display text] stringByAppendingString:digit]];
         userIsInTheMiddleofTypingANumber = YES;
    }
    else {
        [display setText:digit];
        userIsInTheMiddleofTypingANumber = YES;
    }
}

我认为这应该有效。看起来你在循环中首先附​​加数字,即使输入了小数。我没有在编译器中尝试过这段代码,但它检查是否先输入了小数。

答案 1 :(得分:2)

不要让用户感到沮丧。向他们提供有关您能够和不能接受的内容的反馈意见:

- (IBAction)digitPressed:(UIButton *)sender
{
    NSString *char = [[sender titleLabel] text];
    if( [char isEqualTo:@"."] ){
        [sender setEnabled:NO];
    }

    [display setText:[[display text] stringByAppendingString:char]];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)replacementRange replacementString:(NSString *)replacementString {
    // Re-enable the button if the decimal point is deleted
    NSRange rangeForPoint = [[display.text substringWithRange:replacementRange] 
                                   rangeOfString:@"."];
    if( NSNotFound != rangeForPoint.location ){
        // You may also want to check that replacementString does not contain a new
        // decimal point, in the case of pasted text, e.g.
        [decimalPointButton setEnabled:YES];
    }
}

delegate methodtextField:shouldChangeCharactersInRange:replacementString:确实是处理受限输入的关键。您还应该使用isPartialStringValid...方法查看NSFormatter,它可以采用格式并根据用户类型验证输入。

答案 2 :(得分:1)

您可能想查看我之前的帖子,该帖子涉及同一问题。我尝试创建一个接受有限输入的文本字段(例如,只有以下格式的数字:0.00)。它从来没有完全奏效(我相信它有一些货币的错误),但它可能足以满足你的目的:

Re-Apply currency formatting to a UITextField on a change event

答案 3 :(得分:1)

这对我有用:

- (IBAction)digitPressed:(UIButton *)sender
{
    NSString *digit = [[sender titleLabel] text];

    if (userIsInTheMiddleOfTypingANumber)
    { 
        if (decimalEntered)
        {
            NSRange range = [digit rangeOfString:@"."];
            if (range.location == NSNotFound)
            {
                [display setText:[[display text] stringByAppendingString:digit]];
            }
            else
            {
                NSLog(@"You can't enter more than one decimal");
            }
        }
        else if (!decimalEntered)
        {
            NSRange range = [digit rangeOfString:@"."];
            if (range.location == NSNotFound)
            {
                [display setText:[[display text] stringByAppendingString:digit]];
            }
            else 
            {
                [display setText:[[display text] stringByAppendingString:digit]];
                decimalEntered = YES;
            }
        }
    }
    else 
    { 
        decimalEntered = NO;
        [display setText:digit];
        userIsInTheMiddleOfTypingANumber = YES; 
    }
}

答案 4 :(得分:0)


请尝试以下更新的代码

- (IBAction)digitPressed:(UIButton *)sender
{
    NSString *digit = [[sender titleLabel] text];
    if (userIsInTheMiddleofTypingANumber)  
    {
         // if the user enters a decimal point, check if the existing display text contains a "." if not, then allow it, but if it does, do not allow it.
         NSString *decimal = @".";
         NSRange range = [digit rangeOfString:decimal];
         if (range.location == NSNotFound)  
         {
             [display setText:[[display text] stringByAppendingString:digit]]; 
             [display setText:digit];
         }
    }
    else
    {
        [display setText:digit];
        userIsInTheMiddleofTypingANumber = YES;
    }
}