如何检测文本字段中的任何文本更改?委托(delegate)方法 shouldChangeCharactersInRange
适用于某些东西,但它并不能完全满足我的需要。因为在它返回 YES 之前,其他观察者方法无法使用 textField 文本。
例如在我的代码中 calculateAndUpdateTextFields
没有得到更新的文本,用户输入了。
他们有什么方法可以得到 textChanged
Java 事件处理程序。
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
if (textField.tag == kTextFieldTagSubtotal
|| textField.tag == kTextFieldTagSubtotalDecimal
|| textField.tag == kTextFieldTagShipping
|| textField.tag == kTextFieldTagShippingDecimal)
{
[self calculateAndUpdateTextFields];
}
return YES;
}
最佳答案
来自 proper way to do uitextfield text change call back :
I catch the characters sent to a UITextField control something like this:
// Add a "textFieldDidChange" notification method to the text field control.
在 Objective-C 中:
[textField addTarget:self
action:@selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
在 swift 中:
textField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
Then in the
textFieldDidChange
method you can examine the contents of the textField, and reload your table view as needed.
您可以使用它并将 calculateAndUpdateTextFields 作为您的 selector
。
https://stackoverflow.com/questions/7010547/