Ios 键盘隐藏通知多次调用
先生,我正在隐藏键盘Ios 键盘隐藏通知多次调用,ios,objective-c,iphone,Ios,Objective C,Iphone,先生,我正在隐藏键盘 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } 他们工作得很好。但当我们第二次编辑textView时。 [[NSNotificationCenter defaultCenter]addObserver:自选择器:@selector(keyboardWillBeHidden:)名称:UIKeyboardWillHideNot
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
他们工作得很好。但当我们第二次编辑textView时。
[[NSNotificationCenter defaultCenter]addObserver:自选择器:@selector(keyboardWillBeHidden:)名称:UIKeyboardWillHideNotification对象:nil]代码>多次呼叫。他们在键盘上执行我的代码5-6次。是他们在textview开始编辑时不调用的任何方式
- (void)textViewDidBeginEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@"Type your message Here..."]) {
textView.text = @"";
textView.textColor = [UIColor grayColor]; //optional
}
[self scrollToBottomAnimated:YES];
}
- (void)scrollToBottomAnimated:(BOOL)animated
{
NSInteger rows = [tableViews numberOfRowsInSection:0];
if(rows > 0) {
[tableViews scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rows - 1 inSection:0]
atScrollPosition:UITableViewScrollPositionBottom
animated:animated];
}
}
这取决于您在NSNotificationCenter注册键盘通知的位置和次数
如果要在视图中添加观察者,则将显示,在视图中删除观察者将消失
如果要在viewdidload中添加观察者,请在dealloc调用中删除观察者
请尝试更改您注册通知的位置。要确保只有可视控制器是接收通知的控制器,请在vieWillAppear中注册通知,并删除ViewWillEnglish中的通知
这也将解决多次调用代码的问题。在添加通知中心的位置尝试此操作