UITextField值更改事件?

我怎样才能使得当一个文本字段的内容改变时,一个函数被调用?

[myTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; 

实际上, UITextField没有值更改事件,使用UIControlEventEditingChanged

我解决了更改shouldChangeChractersInRange行为的问题。 如果您返回NO,则这些更改将不会由iOS内部应用,而是有机会手动更改并在更改后执行任何操作。

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { //Replace the string manually in the textbox textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string]; //perform any logic here now that you are sure the textbox text has changed [self didChangeTextInTextField:textField]; return NO; //this make iOS not to perform any action } 

为了迅速,这来了方便 –

 textField.addTarget(self, action: #selector(onTextChange), forControlEvents: UIControlEvents.EditingChanged)