5

I want to know the current input character the user just inputted.Comparing the old and the new input string seems to work, but it must be the last thing I'd like to try.Any suggestion? I guess there are some methods in iOS SDK that can do this in a better way.

anna
  • 662
  • 5
  • 28
  • `- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)` the last parameter string is the current input! – anna Apr 21 '12 at 13:48

3 Answers3

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

from the UITextFieldDelegate should help you.

It is not only called for replacing text, also whenever the user presses a key on keyboard. (length of range will be 0 then and the location will be the current insertation position).

See the documentation for more information: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/doc/uid/TP40006991-CH3-SW9

Thyraz
  • 2,412
  • 2
  • 21
  • 23
3

If you want to compare the strings as the characters come in, your better off using

[textField addTarget:self action:@selector(compareInput) forControlEvents:UIControlEventEditingChanged];

-(void)compareInput
{
    if ([textField.text isEqualToString:compareString])
        NSLog(@"They're the same!");
}

than the delegate:

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

If you literally just want to compare characters, then the delegate method is fine. The variable string contains the typed character.

For example, this is what happens with the above methods when a user types foo into the UITextField:

User types: f, this happens:

[textField addTarget:self action:@selector(compareInput) forControlEvents:UIControlEventEditingChanged];

calls compareInput immediately, and the letter f is available via textField.text:

-(void)compareInput
{
    NSLog(textField.text); //prints `f`
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(textField.text); //prints nothing
    return YES;
    //after returning YES, textField.text will contain `f`
}

User types: o, this happens:

[textField addTarget:self action:@selector(compareInput) forControlEvents:UIControlEventEditingChanged];

calls compareInput immediately, and the string fo is available via textField.text:

    -(void)compareInput
{
    NSLog(textField.text); //prints `fo`
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(textField.text); //prints `f`
    return YES;
    //after returning YES, textField.text will contain `fo`
}

I'm probably not very clear, but I hope it gives you some insight!

Tom Redman
  • 5,592
  • 4
  • 34
  • 42
  • 1
    @Thyraz `- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { }` I want to know the current input character, and this method helps a lot. @TRedman thanks for insighting me more. – anna Apr 21 '12 at 07:47
0

This method will show current input in UITextField

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *letters;
    if ([textField.text isEqualToString:@""]){
        letters = string;
    }else if ([string isEqualToString:@""]){
        if ([textField.text length] > 0) {
            letters = [textField.text substringToIndex:[textField.text length] - 1];
        }
    }else{
        letters = [textField.text stringByAppendingString:string];
    }

    NSLog(@"letters %@\n", letters);

    return YES;
}
Michal Gumny
  • 1,770
  • 1
  • 16
  • 24