0

I am trying to use UIControlEventEditingChanged to register for updates whenever the text changes in my UITextField occurs. However something really odd is happening, the handlefieldTextChanged is only called when I delete a character in my UITextField and not when I type something.

This is my code:

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

- (IBAction)handlefieldTextChanged {
    NSLog(@"handleChange");
}

Now the handlefieldTextChanged is declared in the header file and is an IBAction just to see if connecting it in Interface Builder would fix anything but it doesn't.

Anyway what is causing this to happen? Whatever I try I can't get true textfield changed updates.

Edit1:

- (NSString *)removeBadWordsFromString:(NSString *)string {
    for (NSString * word in badWords){
        string = [string stringByReplacingOccurrencesOfString:word withString:@"" 
                                                      options:NSCaseInsensitiveSearch range:NSMakeRange(0, string.length)];
    }
    return string;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    [textField setText:[self removeBadWordsFromString:[NSString stringWithFormat:@"%@%@", textField.text, string]]];
    return (string.length == 0);
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191
  • Have you implemented the `shouldChangeCharactersInRange:` delegate method? If so, is that returning NO? – rmaddy Nov 15 '12 at 00:14
  • I have, if it does return NO, would that be the cause of this problem? The only problem with doing my code in the shouldChangeCharactersInRange: method is that it is called before it registers the click so it is always one late on the clicks which is what I don't want. – SimplyKiwi Nov 15 '12 at 00:16
  • If you return `NO` from `shouldChangeCharactersInRange:`, then the change is rejected so the text field isn't updated. Therefore the control event isn't fired. – rmaddy Nov 15 '12 at 00:22
  • Take a look at my edit1, I pretty much am doing some basic bad word filtering by checking words from a txt file but on top of that I also want to be able to receive UIControlEvents, would that be possible? – SimplyKiwi Nov 15 '12 at 00:29
  • 1
    Two problems. 1) Never return YES from `shouldChangeCharactersInRange:` if you explicitly call `setText:`. 2) When you explicitly call `setText:`, the control event isn't sent. This is to avoid infinite loops incase you need to call `setText:` from your event handler. The solution is for you to explicitly call your `handlefieldTextChanged` method when you call `setText:`. – rmaddy Nov 15 '12 at 00:36
  • One other thing - filtering words as you type is a bad idea. What happens if your bad word list contains "bird" and I want to type "birdie"? – rmaddy Nov 15 '12 at 00:38
  • I would like to keep the idea since the text field is for games and it is a Name field so I wouldn't see how it would interfere at all. I doubt most users would even notice. – SimplyKiwi Nov 15 '12 at 00:59
  • Also I do setText every time so are you saying I should do an if check if it is @"" or not and then do it that way? – SimplyKiwi Nov 15 '12 at 01:01
  • I'm saying that if you call `setText:` in your `shouldChangeCharactersInRange:` method then you must return `NO`. If you don't call it, return `YES`. The return value is telling the framework whether it should proceed with the change or not. It makes no sense for you to make the change yourself and also tell the framework to still make the change. – rmaddy Nov 15 '12 at 01:05
  • Gotcha so since I do it every time shouldn't I just so it to NO all the time? If no bad words are detected it just sets the text to the text that is current in the textField otherwise it returns @"". So I am just confused if you mean to return NO all the time or only when it returns NOT @"" – SimplyKiwi Nov 15 '12 at 01:11

0 Answers0