UITextField 值更改事件?

发布于 2024-09-28 17:59:09 字数 29 浏览 2 评论 0原文

如何才能在文本字段的内容发生更改时调用函数?

How can I make it so that when the contents of a text field changes, a function is called?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空名 2024-10-05 17:59:09

Objective-C

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

Swift 4

myTextField.addTarget(self, action: #selector(textFieldDidChange(sender:)), for: .editingChanged)

@objc func textFieldDidChange(sender: UITextField) {...}

Objective-C

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

Swift 4

myTextField.addTarget(self, action: #selector(textFieldDidChange(sender:)), for: .editingChanged)

@objc func textFieldDidChange(sender: UITextField) {...}
滥情空心 2024-10-05 17:59:09

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

Actually, there is no value changed event for UITextField, use UIControlEventEditingChanged

零崎曲识 2024-10-05 17:59:09

我解决了更改 shouldChangeChractersInRange 行为的问题。如果您返回“否”,则 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
}

I resolved the issue changing the behavior of shouldChangeChractersInRange. If you return NO the changes won't be applied by iOS internally, instead you have the opportunity to change it manually and perform any actions after the changes.

- (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
}
尘曦 2024-10-05 17:59:09

这是我的解决方案。

斯威夫特4

textField.addTarget(self, action: #selector(self.textFieldDidChange(sender:)), for: .editingChanged)

@objc func textFieldDidChange(sender: UITextField){
   print("textFieldDidChange is called")
}

this is my solution.

Swift 4

textField.addTarget(self, action: #selector(self.textFieldDidChange(sender:)), for: .editingChanged)

@objc func textFieldDidChange(sender: UITextField){
   print("textFieldDidChange is called")
}
原谅我要高飞 2024-10-05 17:59:09

对于 swift 来说这很方便 -

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

For swift this comes handy -

textField.addTarget(self, action: #selector(onTextChange), forControlEvents: UIControlEvents.EditingChanged)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文