ios - UITextField 文本更改事件

如何检测文本字段中的任何文本更改?委托(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/

相关文章:

objective-c - 如何检查 NSDictionary 或 NSMutableDiction

ios - 在 ARC 下,IBOutlets 应该强还是弱?

objective-c - 如何在 Xcode 4 中设置 NSZombieEnabled?

ios - 如何将 UIButton 的标题设置为左对齐?

objective-c - 如何将百分号添加到 NSString

ios - 设置 UITextField 的最大字符长度

objective-c - 如何打印出方法名称和行号并有条件地禁用 NSLog?

objective-c - 在 Objective-C 中创建一个抽象类

ios - 如何以编程方式在 iPhone 上发送短信?

ios - 如何浏览文本字段(下一步/完成按钮)