加速键盘解雇或等到它完成

时间:2011-11-29 06:33:45

标签: iphone ios wait uinavigationitem resignfirstresponder

我的模式UINavController中的保存按钮出现问题。当我按下保存按钮时,我正在关闭键盘,如果它仍在运行,验证文本字段中的数据,然后在我发送信息时显示UIProgressView。

我的问题是键盘没有足够快的速度,所以当显示UIProgressView并且它被添加到我视图的底部并且看起来很愚蠢时,键盘仍然处于运行状态。< / p>

我可以点击返回键,键盘掉线,然后按保存,没有问题。但是如果用户跳过键盘返回键并向右移动右上方的保存按钮,我就会遇到问题。

理想情况下,我想实施一个简短的等待声明,让它远离视线。或者在延迟后执行我的验证,但我尝试过的任何工作都没有。请帮忙。

代码示例:

// end edit mode - should kill all keyboards
[[self.tableView superview] endEditing:YES]; 

// make sure everything is entered correctly and validates
[self validateEntryFields]; // keyboard not gone when this finishes

if (valid) { // progress view shows up towards bottom of view
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.delegate = self;
    HUD.labelText = @"Adding User";
    HUD.detailsLabelText = @"Please Wait";
    [HUD showWhileExecuting:@selector(sendNewUserInformation) onTarget:self withObject:nil animated:YES];
}

2 个答案:

答案 0 :(得分:3)

您可以使用键盘通知(这将是比基于计时器更正确的解决方案):

[notificationCenter addObserver: self selector: @selector(keyboardDidHide:) name: UIKeyboardDidHideNotification object: nil];

并在keyboardDidHide:方法中显示您的进度视图。

答案 1 :(得分:1)

如果稍有延迟,您可以在验证和if语句之间尝试以下代码,您可以在其中决定是否显示进度视图。

您可以使用NSTimer对象:

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(mumboJumbo:)userInfo:nil repeats:NO];

并将您的代码添加到以下方法中:

-(void)mumboJumbo:(id)sender{
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.delegate = self;
    HUD.labelText = @"Adding User";
    HUD.detailsLabelText = @"Please Wait";
    [HUD showWhileExecuting:@selector(sendNewUserInformation) onTarget:self withObject:nil animated:YES];
}

如果我理解你的问题应该有效。