在UITextField中停止输入时检测

Max*_*Max 6 objective-c uitextfield ios

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

小智 11

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

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

//
//  ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextFieldDelegate>

@end
Run Code Online (Sandbox Code Playgroud)

然后,实现定时搜索:

//
//  ViewController.m

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textfield;
@property (strong, nonatomic) NSTimer * searchTimer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // declare this view controller as the delegate
    self.textfield.delegate = self;

    // handle the change event (UIControlEventEditingChanged) by calling (textFieldDidChange:)
    [self.textfield addTarget:self
                  action:@selector(textFieldDidChange:)
        forControlEvents:UIControlEventEditingChanged];

}

// reset the search timer whenever the text field changes
-(void)textFieldDidChange :(UITextField *)textField{

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

    // reschedule the search: in 1.0 second, call the searchForKeyword: method on the new textfield content
    self.searchTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0
                                                        target: self
                                                      selector: @selector(searchForKeyword:)
                                                      userInfo: self.textfield.text
                                                       repeats: NO];

}


- (void) searchForKeyword:(NSTimer *)timer
{
    // retrieve the keyword from user info
    NSString *keyword = (NSString*)timer.userInfo;

    // perform your search (stubbed here using NSLog)
    NSLog(@"Searching for keyword %@", keyword);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
Run Code Online (Sandbox Code Playgroud)


Abd*_*rim 11

快速
xcode - 10

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

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

Run Code Online (Sandbox Code Playgroud)


The*_*izo 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)")
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 这正是我想要的 (2认同)