Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out UITextfield's end editing without resigning first responder

When user is typing in UITextfield, and he stops for 2 seconds, the cursor is still on UITextfield, so how we can identify this event? i.e. I want to check the whether the editing is end or not without resigning the first responser from that UITextField. What is the way to do that?

like image 679
prabhu Avatar asked Mar 14 '13 11:03

prabhu


2 Answers

Yes, we can check that! with UITextField delegate, - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

- (void) callMeAfterTwoSeconds {
    NSLog(@"I'll call after two seconds of inactivity!");
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    [NSRunLoop cancelPreviousPerformRequestsWithTarget:self];
    [self performSelector:@selector(callMeAfterTwoSeconds) withObject:nil afterDelay:2.0];

    return YES;
}

While you're typing (tapping keys from keyboard), it will cancel previous calls for the callMeAfterTwoSeconds function, once you stop, it sets it to call after 2 seconds delay, and yes, it will call after 2 seconds.

Update: Even you can pass that textfield as object to performSelector to know which textfield is inactive, for that your callMeAfterTwoSeconds function will be like,

- (void) callMeAfterTwoSeconds:(UITextField *)textfield {
    if(textfield == txtUserName) {
        NSLog(@"User textfield has NO activity from last two seconds!"); }
}
like image 136
Hemang Avatar answered Sep 27 '22 18:09

Hemang


Go to the connections inspector of your UITextField and connect the "Editing Changed" from the Sent Events list to a predefined IBAction of your choice. Alternatively you can do it programmatically if you are not working with Storyboard.

[youTextField addTarget:self action:@selector(textFieldInputDidChange:) forControlEvents:UIControlEventEditingChanged];

Now the IBAction you've just connected will be fired every time the user changes a character in the UITextField. Create a timer as ivar. Now every time IBAction gets called, start the timer, if it will hit 2sec without being restarted by a new call you know the user hasn't entered/deleted values in the UITextField.

like image 25
iDroid Avatar answered Sep 27 '22 20:09

iDroid