【发布时间】:2018-02-06 08:40:36
【问题描述】:
由于某种原因,UIKeyboardWillHideNotification 在我下面的代码中被执行了两次——我不知道为什么。我知道这一点是因为我的 NSLog(“关闭!”)在我的控制台中出现了两次。我是否遗漏了一些明显的东西(不,我没有第二次将 UIKeyboardWillHideNotification 粘贴到我的代码中)。
-(void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboard:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboard:) name:UIKeyboardWillShowNotification object:nil];
}
- (void)handleKeyboard:(NSNotification*)aNotification{
NSDictionary* info = [aNotification userInfo];
NSValue* value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval duration = 3;
[value getValue:&duration];
if (aNotification.name == UIKeyboardWillShowNotification) {
self.upView.frame = CGRectOffset(self.upView.frame, 0, -self.keyboardHeight); self.tableView.frame = CGRectOffset(self.tableView.frame, 0, -self.keyboardHeight);
[self moveCustomView:YES duration:duration];
}
if (aNotification.name == UIKeyboardWillHideNotification) {
/** KEYBOARD HIDE **/
self.upView.frame = CGRectOffset(self.upView.frame, 0, self.keyboardHeight); self.tableView.frame = CGRectOffset(self.tableView.frame, 0, self.keyboardHeight);
[self moveCustomView:NO duration:duration];
NSLog(@"CLOSED!");
}
}
【问题讨论】:
-
无关备注:请使用
isEqual:或isEqualToString:比较字符串。==导致指针比较,并可能导致错误(假阴性)。 -
我已复制您的代码并粘贴到我的项目中,但它对我有用。没有任何问题。只调用了一次。
-
点击文本字段
UIKeyboardWillShowNotification调用1次..返回文本字段UIKeyboardWillHideNotification调用1次.. -
多么奇怪@NiravKotecha - 也许我在不同的视图控制器中有一些东西会影响这个?可能的?虽然我不认为 UIKeyboardWillHide 会延续到另一个视图?
-
监听键盘是全局的,所以当键盘显示在另一个监听它的视图控制器中时,如果它没有被释放,那个视图控制器就会打印出来
标签: ios objective-c