UITextField文本更改事件 [英] UITextField text change event

查看:139
本文介绍了UITextField文本更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测textField中的任何文本更改?委托方法 shouldChangeCharactersInRange 适用于某些东西,但是它并没有完全满足我的需要。由于直到它返回YES,textField文本不适用于其他观察器方法。

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.

例如在我的代码 calculateAndUpdateTextFields 中没有获得更新的文本,用户已输入。

e.g. in my code calculateAndUpdateTextFields did not get the updated text, the user has typed.

他们有任何方式获取类似于 textChanged Java事件处理程序的东西。

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;
}


推荐答案

正确的做法uitextfield文本更改回调


我捕获发送到UITextField控件的字符,如下所示:

I catch the characters sent to a UITextField control something like this:



// Add a "textFieldDidChange" notification method to the text field control.
[textField addTarget:self 
              action:@selector(textFieldDidChange:) 
    forControlEvents:UIControlEventEditingChanged];




然后在textFieldDidChange:方法中,您可以检查textField的内容,并根据需要重新载入您的表视图。

Then in the textFieldDidChange: method you can examine the contents of the textField, and reload your table view as needed.

您可以使用它,并将 calculateAndUpdateTextFields 作为 selector

You could use that and put calculateAndUpdateTextFields as your selector.

这篇关于UITextField文本更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆