1

I am working on an application in iOS that displays a random string to the user, and asks the user to enter that same string through the keypad. What I would like to do is compare the string the user has entered, ONLY after they have pressed the 'return' key. Is this possible?

Here is my relevant method that I am doing this in:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {


    if (textField.tag == 1)
    {

            userString = @"";
            userString = keypadText.text;

            [keypadText resignFirstResponder];

           //here 'rand' is the random string that was presented earlier to the user, and userString is the text that the user has entered.
            if ([userString isEqualToString:rand]) {

                //do something

            }

            else {

                //do something else

            }

            [self.navigationController popViewControllerAnimated:YES];


    }

    return YES;

}

As I said, I ONLY want to do the comparison after the user has pressed the 'return' key, and up until then, allow the user as much opportunity to enter the given text, and make whatever corrections necessary.

Can someone show me how to do this?

Yusubov
  • 5,650
  • 9
  • 29
  • 68
syedfa
  • 2,707
  • 1
  • 34
  • 71
  • Possible duplicate of http://stackoverflow.com/questions/976861/uitextfield-capture-return-button-event – nebs Jan 17 '13 at 20:27

1 Answers1

6

Just use textFieldDidFinishingEditing instead of shouldChangeCharactersinRange.

Of course, there is also

- (BOOL)textFieldShouldReturn:(UITextField *)textField

The latter is the better way.

So simple.

Josiah
  • 4,283
  • 2
  • 28
  • 49