【发布时间】:2017-02-03 17:29:05
【问题描述】:
我有一个带有自定义视图的 xib 文件,上面有许多文本字段子视图。我已将每个文本字段上的委托设置为文件所有者,并成功地能够使用 texfield 委托方法。但不幸的是,键盘通知方法keyboardWillShow:和keyboardWillHide:根本没有被调用。
我在 textfieldShouldBeginEditing 添加了观察者,并在 textFieldDidEndEditing 移除了观察者。
这是我的代码的 sn-p:
添加观察者
-(void) textFieldShouldBeginEditing : (UITextField *) textField{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
移除观察者
-(void) textFieldDidEndEditing : (UITextField *) textField{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
//通知方法
- (void)keyboardWillShow:(NSNotification *)notification
{
NSLog(@"Here");
}
- (void)keyboardWillHide:(NSNotification *)notification
{
NSLog(@"There");
}
//这些keyboardwillShow和keyboardWillHide中的任何一个都不会被调用。
如果有人能帮我找出我的代码问题或我可能遗漏的任何潜在问题,那就太好了。
【问题讨论】:
-
有可能在
textFieldDidBeginEditing:被调用之前键盘显示,导致keyboardWillShow永远不会被调用。早点开始观察,看看是否有任何改变
标签: ios objective-c uikeyboard nsnotification