繁体   English   中英

如何获取 UITextField 的 textDidChange(如 UISearchBar)方法?

[英]How can I get a textDidChange (like for a UISearchBar) method for a UITextField?

如何获取 UITextField 的 textDidChange 方法? 每次对文本字段进行更改时,我都需要调用一个方法。 这可能吗? 谢谢!

每当文本字段更改时,您可以使用UITextFieldTextDidChangeNotification调用方法。 将此添加到您的init

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextFieldTextDidChangeNotification object:nil];

您可以设置委托并使用

- (BOOL) textField: (UITextField *) textField shouldChangeCharactersInRange: (NSRange) range replacementString: (NSString *) string;

您还可以为UIControlEventEditingChanged事件设置目标和选择器。 例如:

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

请注意,这只会在用户对文本字段进行更改时调用,并且不会在以编程方式设置文本时自动调用。

进一步详细说明上面 Ben Gottlieb 的回答,使用 textField shouldChangeCharactersInRange 很好,但缺点是某些事情可能会因一个字符的延迟而发生。

例如,当您输入一个字符并且它不再为空时,实际上在没有文本时调用下面的字符来提醒您之后的字符。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if (!textField.text.length) {
        // Do something with empty textfield
    }
    return YES;
 }

下面的方法允许您在 textField 中进行基本更改。 虽然不像 NSNotification 那样直接,但它仍然允许您对不同的文本字段使用类似的方法,因此在尝试获取文本字段中的特定字符更改时使用它非常有用。

下面的代码修复了字符延迟

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    // This means it updates the name immediately
    NSString * newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    textField.placeholder = newString.length ? @"" : @"Name";

    return YES;
 } 

因为此代码用于在 textField 中没有文本时向 UITextField 添加占位符

当您在委托 shouldChangeCharactersInRange 中使用replaceCharactersInRange时,我发现了一些问题。

例如:在越南语键盘字符串aa -> â中,因此如果您使用方法replaceCharactersInRange则结果不正确。

在这种情况下,您可以通过事件UIControlEventEditingChanged尝试:

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

迅捷解决方案

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let newString = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
    return true
}

下面是我使用的 Swift 4

public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    guard let fieldText = textField.text else {
        return false
    }
    // Append the existing textfield text with the new character
    var text = fieldText + string
    // If it is the very first character, textfield text will be empty so take the 'string' and on delete also textfield text get deleted with a delay
    if range.location == 0{
       text = string
    }
    return true
}

简单地:

class MTextField: NSTextField {
    override func textDidChange(_ notification: Notification) {
        //...
    }
}

在 Obj-C 中(不检查):

@interface  MTextField: NSTextField
@end
@implementation  MTextField
- (void) textDidChange:(NSNotification*) notification {
    //...
}
@end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM