UITextField 文本更改事件 [英] UITextField text change event
问题描述
如何检测 textField 中的任何文本更改?委托方法 shouldChangeCharactersInRange
对某些事情有效,但它并不能完全满足我的需要.因为在它返回 YES 之前,textField 文本对其他观察者方法不可用.
例如在我的代码 calculateAndUpdateTextFields
中没有得到更新的文本,用户已经输入.
他们有什么方法可以得到类似 textChanged
Java 事件处理程序的东西.
- (BOOL)textField:(UITextField *)textFieldshouldChangeCharactersInRange:(NSRange)range替换字符串:(NSString *)字符串{if (textField.tag == kTextFieldTagSubtotal||textField.tag == kTextFieldTagSubtotalDecimal||textField.tag == kTextFieldTagShipping||textField.tag == kTextFieldTagShippingDecimal){[自我计算和更新文本字段];}返回是;}
来自 正确的方法来做 uitextfield 文本更改回调:
<块引用>我捕获发送到 UITextField 控件的字符,如下所示:
//在文本字段控件中添加textFieldDidChange"通知方法.
在 Objective-C 中:
[textField addTarget:self动作:@selector(textFieldDidChange:)forControlEvents:UIControlEventEditingChanged];
在 Swift 中:
textField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
<块引用>
然后在 textFieldDidChange
方法中,您可以检查 textField 的内容,并根据需要重新加载表格视图.
您可以使用它并将 calculateAndUpdateTextFields 作为您的 selector
.
How can I detect any text changes in a textField? The delegate method shouldChangeCharactersInRange
works for something, but it did not fulfill my need exactly. Since until it returns YES, the textField texts are not available to other observer methods.
e.g. in my code calculateAndUpdateTextFields
did not get the updated text, the user has typed.
Is their any way to get something like textChanged
Java event handler.
- (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;
}
From 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.
In Objective-C:
[textField addTarget:self
action:@selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
In 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.
You could use that and put calculateAndUpdateTextFields as your selector
.
这篇关于UITextField 文本更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!