我有一个UITextView(目的:注释)固定在我的屏幕底部,当用户想要添加评论时,键盘出现,我将评论视图与评论一起向上移动。我还有一个取消按钮来隐藏键盘,但键盘没有隐藏
//Set up NSNotification for Keyboard
-(void) viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillToggle:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillToggle:)
name:UIKeyboardWillHideNotification object:nil];
}
//Code to shift comment view up with keyboard
- (void) keyboardWillToggle:(NSNotification *)aNotification
{
CGRect frame = [self.navigationController.toolbar frame];
CGRect keyboard = [[aNotification.userInfo valueForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
frame.origin.y = keyboard.origin.y - frame.size.height;
[UIView animateWithDuration:[[aNotification.userInfo valueForKey:@"UIKeyboardAnimationDurationUserInfoKey"] floatValue] animations:^
{
[self.navigationController.toolbar setFrame:frame];
}];
}
//Hide keyboard
-(void)cancelComment:(UIBarButtonItem*)sender{
NSLog(@"cancelComment called");
[self.view endEditing:YES];
}
我觉得这应该有效吗?正在将“cancelComment called”记录到控制台,但键盘未隐藏
答案 0 :(得分:2)
<强> SOLUTION:强>
你忘记了:
repokey
你的cancelComment函数中的。
答案 1 :(得分:1)
你可以尝试
-(void)cancelComment:(UIBarButtonItem*)sender{
NSLog(@"cancelComment called”);
[self.navigationController.view endEditing:YES];
}
我认为你的textView不在self.view和self.navigationController.view
中答案 2 :(得分:0)
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[yourtextfieldname resignfirstresponder];
}
希望你能来这个!