iOS:shouldChangeCharactersInRange返回旧结果

时间:2014-03-03 21:32:21

标签: ios textfield

我有一个简单的UItextField。在这个我想只有4个数字(字符串)。如果此字符串不等于另一个字符串,则应清除输入(完美地工作)。但是在shouldChangeCharactersInRange中,最后一个数字仍然存在并代表新字符串中的第一个数字。

如何清除所有内容并将shouldChangeCharactersInRange设置为基本位置?

CODE

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == passcodeTextfield)
    {
        NSString *passcode = [textField.text stringByReplacingCharactersInRange:range withString:string];

        if (passcode.length == 0)
        {
            firstPasscodeDigitTextfield.text = @"";
        }
        else if (passcode.length == 1)
        {
            firstPasscodeDigitTextfield.text = @"\u25cf";
            secondPasscodeDigitTextfield.text = @"";
        }
        else if (passcode.length == 2)
        {
            secondPasscodeDigitTextfield.text = @"\u25cf";
            thirdPasscodeDigitTextfield.text = @"";
        }
        else if (passcode.length == 3)
        {
            thirdPasscodeDigitTextfield.text = @"\u25cf";
            fourthPasscodeDigitTextfield.text = @"";
        }
        else if (passcode.length == 4)
        {
            fourthPasscodeDigitTextfield.text = @"\u25cf";

            [self verifyPasscode:passcode];
        }
        else if (passcode.length > 4)
        {
            return NO;
        }
    }

    return YES;
}

- (void)verifyPasscode:(NSString *)passcode
{
    if ([passcode isEqualToString:@"4277"])
    {
        [passcodeTextfield resignFirstResponder];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else
    {
        [passcodeTextfield resignFirstResponder];
        [passcodeTextfield becomeFirstResponder];
        [self textFieldShouldClear:passcodeTextfield];
    }
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    if (textField == passcodeTextfield)
    {
        passcodeTextfield.text = @"";
        firstPasscodeDigitTextfield.text = @"";
        secondPasscodeDigitTextfield.text = @"";
        thirdPasscodeDigitTextfield.text = @"";
        fourthPasscodeDigitTextfield.text = @"";
    }

    return YES;
}

1 个答案:

答案 0 :(得分:2)

我认为你应该这样做:

1)当您为点创建视图(firstPasscodeDigitTextfield等)时,将标记设置为它们:0,1,2,3

2)更改

时从passwordTextField获取整个字符串值

3)获取字符串的长度

4)实现功能:

-(void)setNpointsToView:(NSUInteger)n
{
    //getting your views and set point to them
    for (int i = 0; i < n; ++i)
    {
        [[self.view viewWithTag:i] setText: @"\u25cf"]; 
    }
    //set "" to other views
    for (int i = n; i < 3; ++i)
    {
        [[self.view viewWithTag:i] setText" @""];
    }
}

5)每次使用((字符串的长度) - 1)更改passwordTextField中的文本作为参数

时调用此函数

6)实施:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == passcodeTextfield)
    {
        NSString *passcode = [textField.text stringByReplacingCharactersInRange:range withString:string];

        if (passcode.length > 3)
        return NO;

    }
}