我的 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/