簡體   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