在UITextField中停止输入时检测

时间:2015-07-05 04:08:14

标签: ios objective-c uitextfield

如何检测UITextField中的输入是否已停止?我应该使用某些种类的UITextFieldTextDidEndEditingNotification功能吗?我试图创建一个像搜索一样的Instagram,它会在一秒钟没有输入后显示结果。

4 个答案:

答案 0 :(得分:11)

此方法使用NSTimer在文本字段更改后1秒钟安排搜索。如果在该秒之前输入了新字符,则计时器重新开始。这样,搜索仅在最后一次更改后开始触发。

首先,确保ViewController符合UITextFieldDelegate协议:

.hero {
  z-index: -1;
  position: fixed;
  top: 50%;
  left: 50%;
  min-width: 100%;
  min-height: 100%;
  transform: translateX(-50%) translateY(-50%);
  max-width: 100%;
}

.container {
    width: 100%;
    overflow: hidden;
}

然后,实施定时搜索:

//
//  ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextFieldDelegate>

@end

答案 1 :(得分:9)

这就是我的做法,它适用于 Swift 3

import UIKit

// don't forget to add UITextFieldDelegate
class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var textField: UITextField!

    var searchTimer: Timer?

    override func viewDidLoad() {
        super.viewDidLoad()

        // declare textField as delegate
        self.textField.delegate = self

        // handle the editingChanged event by calling (textFieldDidEditingChanged(-:))
        self.textField.addTarget(self, action: #selector(textFieldDidEditingChanged(_:)), for: .editingChanged)
    }

    // reset the searchTimer whenever the textField is editingChanged
    func textFieldDidEditingChanged(_ textField: UITextField) {

        // if a timer is already active, prevent it from firing
        if searchTimer != nil {
            searchTimer?.invalidate()
            searchTimer = nil
        }

        // reschedule the search: in 1.0 second, call the searchForKeyword method on the new textfield content
        searchTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(searchForKeyword(_:)), userInfo: textField.text!, repeats: false)
    }

    func searchForKeyword(_ timer: Timer) {

        // retrieve the keyword from user info
        let keyword = timer.userInfo!

        print("Searching for keyword \(keyword)")
    }

}

答案 2 :(得分:1)

试试这段代码:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSDate* timeStamp = [NSDate date];
    _timeStamp = timeStamp;
    CGFloat END_TYPING_TIME = 1.5;
    [self performSelector:@selector(endTyping:) withObject:timeStamp afterDelay:END_TYPING_TIME];
    return YES;
}

-(void) endTyping:(NSDate*) timeStamp {
    if ([timeStamp isEqualToDate:_timeStamp]) { //if it is the last typing...
        //TODO: do what ever you want to do at the end of typing...
    }
}

确定您案件的END_TYPING_TIME是什么......

_timeStampNSDate类型的字段。

答案 3 :(得分:1)

迅速
xcode-10

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange,
    replacementString string: String) -> Bool {
    NSObject.cancelPreviousPerformRequests(
        withTarget: self,
        selector: #selector(ViewController.getHintsFromTextField),
        object: textField)
    self.perform(
        #selector(ViewController.getHintsFromTextField),
        with: textField,
        afterDelay: 0.5)
    return true
}

func getHintsFromTextField(textField: UITextField) {
    print("Hints for textField: \(textField)")
}