此功能用于模拟用户成功登录时的等待。如您所见,我首先关闭键盘,但在解除键盘之前,NSThread不会停止睡眠。我想我需要利用调度队列的力量但不太确定。我有什么办法可以在睡眠前解开键盘?
-(IBAction)userLoginButtonPressed:(id)sender
{
/* resign first responders so that the user
can see the label of the server trying to log in */
[self.usernameField resignFirstResponder];
[self.passwordField resignFirstResponder];
self.statusLabel.text = @"Logging In...";
// create the server object and pass in the username and password values
IONServer *server = [[IONServer alloc] init];
NSString *user = self.usernameField.text;
NSString *pw = self.passwordField.text;
[server loggingInWithUserName:user password:pw];
// redirect based on result
if (server.result.success) {
[self serverSuccess];
[NSThread sleepForTimeInterval:2.0f];
} else {
[self serverFailure];
}
// store the server object as this class' server var
self.server = server;
NSLog(@"Result From Server: %@", server.result);
}
答案 0 :(得分:3)
解雇键盘是动画的,这足以让我们知道它在主线程上发生异步。换句话说 - 你的代码块启动,解雇被添加到主线程runloop的键盘,线程会因为你这么说而睡2秒钟(如果你问我可怕的事情),而且只有它的键盘和#39;轮到动画并被解雇。
一个好方法可以是
[UIView animateWithDuration:0 animations: ^{
[self.usernameField resignFirstResponder];
[self.passwordField resignFirstResponder];
} completion: ^(BOOL finished) {
// Do whatever needed...
[NSThread sleepForTimeInterval:2.0f];
}];
但是 - 我强烈建议找一个比冻结主线程更好的解决方案。
此外 - 没有您要求的内容,但是,假设您的所有文字字段都是self.view
的子视图,则只需拨打[self.view endEditing:YES];
即可,您无需关心哪个文字字段为响应者。