iOS keyboard delegate callback function
I've got a function set up to run when the return key is tapped on an textfield keypad, in an iOS app I'm writing.
The function is quite slow, and takes about开发者_运维知识库 15 - 20 seconds to run, during which time the whole screen just freezes up, the keypad hangs on the screen and nothing happens.
Only when the function finishes running does the keypad disappear.
The XIB file has "Editing Did End" set to "searchPlates", the funciton name, and it all works fine, I just want the keyboard to disappear, so I can show a UIProgressView while the search is happening.
I've also got the following code in the view controller:
-(BOOL)textFieldShouldReturn:(UITextField *)thisTextField {
if (thisTextField == self.plateInput) {
[thisTextField resignFirstResponder];
}
return YES;
}
Is that clear enough? I want the keypad to disappear straight away when searchPlates is called.
You should avoid long-running functions like yours directly in the main run loop. Instead, perform the work that takes 15-20 seconds in a new thread. You can display a progress view when you trigger that work and hide it when that work completes.
See How do I update a progress bar in Cocoa during a long running loop? for specific examples of how you might want to do this.
精彩评论