Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextFieldTextDidChangeNotification iOS7 not fired

I'm using this piece of code to get informed about changing text of a UITextField.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textUpdated) name: UITextFieldTextDidChangeNotification object:self.inputValueField.text];

Works fine with iOS6 but is not getting called with iOS7. Any ideas?

like image 710
netshark1000 Avatar asked Dec 02 '25 04:12

netshark1000


2 Answers

The Problem is, that you are passing the wrong object. You are passing the NSString in your UITextField, but should passthe UITextField itself.

[[NSNotificationCenter defaultCenter] addObserver:self 
        selector:@selector(textUpdated)
        name: UITextFieldTextDidChangeNotification
        object:self.inputValueField];

This should work.

like image 109
viirus Avatar answered Dec 06 '25 05:12

viirus


Better using NSNotificationCenter. Here is a code snippet from a passcode managing section of an app.

Initialize NSNotificationCenter in viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name: UITextFieldTextDidChangeNotification object:nil];

then declare its method textDidChange:

- (void)textDidChange:(NSNotification*)notification
{
    UITextField *textField = (UITextField *)[notification object];
    DBG(@"%@", textField.text);
    if(textField == self.txtPasscode1 && textField.text.length == 1)
    {
        [self.txtPasscode2 becomeFirstResponder];
    }
    if(textField == self.txtPasscode2 && textField.text.length == 1)
    {
        [self.txtPasscode3 becomeFirstResponder];
    }
    if(textField == self.txtPasscode3 && textField.text.length == 1)
    {
        [self.txtPasscode4 becomeFirstResponder];
    }
    if(textField == self.txtPasscode4 && textField.text.length == 1)
    {
        [self.txtPasscode1 becomeFirstResponder];
    }
}
like image 40
Vaibhav Saran Avatar answered Dec 06 '25 06:12

Vaibhav Saran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!