iphone - iOS 版本之间的 UIKeyboardWillShowNotification、UIKeyboardWillHideNotification 和 NSNotificationCenter 问题

标签 iphone objective-c ios4 uikeyboard ios-3.x

我的 View 中有几个 UITextField(每个都在一个 UITableViewCell 中)。当从任何文本字段触发键盘时,我需要制作一些动画,主要是更改 UITableView 的框架。当键盘隐藏时,必须发生同样的情况。

我做过动画,所以这不是这里的问题。

现在,我使用 NSNotificationCenter 捕捉键盘的显示/隐藏:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];

问题是当键盘可见(使用文本字段)并且我在另一个文本字段中按下时。通常对于这个东西键盘不会隐藏,但会保持可见。

它在 iOS 4 中工作正常,但问题出现在 3.1.3(这是我可以测试的版本 - 可能是 3.2 以下的任何版本)。在 3.2 之前的版本中,将焦点从一个文本字段直接更改到另一个文本字段将触发 UIKeyboardWillHideNotification 和 UIKeyboardWillShowNotification。

有谁知道在没有 NSNotificationCenter 的情况下键盘真正显示/隐藏时执行某些动画的方法吗?

或者我如何解决低于 3.2 的版本的这个问题?

谢谢。

最佳答案

您可以做的是将文本字段/ TextView 的委托(delegate)设置为当前 View Controller 并实现这两个方法

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    _keyboardWillHide = NO;
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    _keyboardWillHide = NO;
    return YES;    
}

之后,在您的方法中,通过 UIKeyboardWillHideNotification 通知触发您可以执行类似的操作

if (_keyboardWillHide) {
    // No other textfield/textview was selected so you can animate the tableView
    ...
}
_keyBoardWillHide = YES;

让我知道这是否适合您。

关于iphone - iOS 版本之间的 UIKeyboardWillShowNotification、UIKeyboardWillHideNotification 和 NSNotificationCenter 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6795159/

相关文章:

iphone - MPMoviePlayerViewController 在后台运行并使用远程控制

iphone - 模态视图中的淡出过渡

iphone - 如何将plist数据读入数据模型?

ios - UICollectionViewCell 图像中的 UIImageView 不适合

objective-c - EventKit:提醒 dueDateComponents 与 Alarm

objective-c - UIAlertView 仅在关闭后才显示

iphone - 如何使用CABasicAnimation从一个点移动到另一个点?

objective-c - 在 Objective-C 中比较两次的最好/最简单的方法是什么?

objective-c - 未命名变量的内存管理

iphone - 我可以取消“applicationDidEnterBackground”方法吗?