Search code examples
iosobjective-cuitextfield

Detect when stopped typing in UITextField


How should I go about detecting if typing has stopped in a UITextField? Should I use the UITextFieldTextDidEndEditingNotification function of some sorts? I a trying to create a instagram like search where it will display the results after a second of not typing.


Solution

  • This approach uses an NSTimer to schedule a search 1 second after the text field changes. If a new character is typed before that second is up, the timer starts over. This way, the search is only triggered starts after the last change.

    First things first, make sure the ViewController is conforms to the UITextFieldDelegate protocol:

    //
    //  ViewController.h
    
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController<UITextFieldDelegate>
    
    @end
    

    Then, implement the timed search:

    //
    //  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