Question

I have a simple UItextField. In this one I'd like to have only 4 numbers (in string). If this string isn't equal to another string, the input should be cleared (works perfectly). But in shouldChangeCharactersInRange the last number is still existing and represents the first number in the new string.

How can I clear everything and set shouldChangeCharactersInRange to the basic position?

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;
}
Was it helpful?

Solution

I think you should do something like this:

1) when you creating views for points (firstPasscodeDigitTextfield, etc.), set tag to them: 0,1,2,3

2) get whole string value from passwordTextField when she changed

3) get length of the string

4) implement function:

-(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) call this function every time you change text in passwordTextField with ((length of the string) - 1) as parameter

6) Implement:

- (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;

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top