A way to tell when the user is done typing on a timed interval?
I am wondering if there is a way to tell if the user hasn't typed in the UITextField
say for 2 seconds.
If that's not possible, I'd like to know if there is a way to tell if the user made an error typing 开发者_如何学Pythonin the text field - example:
There is a UITextView
which contains objects from an NSArray
and the user has to type in the textfield and match the content on the text view. If they make a mistake, it will tell them (what I need help with) - Note: if the user types the string correctly, it automatically accepts it and goes on to the next one - there is no button to submit the string - it does that via the text field.
Wherever you're handling the change of text, you can start an NSTimer
. If more text is entered before the timer fires, you can call -invalidate
on it to cancel it, but if the timer fires then you know no text has been entered.
This is possible, but it is going to involve some work. You will need to check out the NSTimer and NSDate classes and the UITextFieldDelegate protocol. Basically, every time the user enters something into the text field, you get a delegate method to fire. That method then caches the current time (via [NSDate date]) and starts a timer with a two-second duration. When the timer fires, you have it trigger a method that checks the cached NSDate to see if the last input into the text field was within the last two seconds.
Check out the delegate methods for UITextFile
and UITextView
. There are several methods you can implement that are called when the user changes the state of the field. In addition, you can fire off NSTimer
if you need something for a particular interval.
Register for text changed events on the text field. When a change occurs, create or update a timer member. If the timer fires, your time has elapsed. When cleaning up, invalidate the timer.
-(void) textFieldChanged:(id)sender { // UIControlEventEditingChanged
NSTimeInterval tooLong = 2.0;
if ( myTimer ) [myTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:tooLong]];
else myTimer = [[NSTimer scheduledTimerWithTimeInterval:tooLong ... repeats:NO] retain];
}
精彩评论