UITextFieldTextDidChangeNotification iOS7未被触发

net*_*000 2 uitextfield ios7

我正在使用这段代码来获取有关更改UITextField文本的信息.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textUpdated) name: UITextFieldTextDidChangeNotification object:self.inputValueField.text];
Run Code Online (Sandbox Code Playgroud)

适用于iOS6,但不会通过iOS7调用.有任何想法吗?

vii*_*rus 5

问题是,你传递了错误的对象.您正在UITextField中传递NSString,但是应该使用UITextField本身.

[[NSNotificationCenter defaultCenter] addObserver:self 
        selector:@selector(textUpdated)
        name: UITextFieldTextDidChangeNotification
        object:self.inputValueField];
Run Code Online (Sandbox Code Playgroud)

这应该工作.


Vai*_*ran 5

更好地使用NSNotificationCenter. 这是来自应用程序密码管理部分的代码片段。

初始化NSNotificationCenterviewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name: UITextFieldTextDidChangeNotification object:nil];
Run Code Online (Sandbox Code Playgroud)

然后声明它的方法 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];
    }
}
Run Code Online (Sandbox Code Playgroud)