简体   繁体   English

编辑UItextview时隐藏并显示IOS键盘

[英]Hide and show IOS Keyboard when editing UItextview

I would like to detect when certain UItextField and UItextView are edited, in order to run an animation. 我想检测何时编辑某些UItextField和UItextView以便运行动画。 I approached the problem in this way: 我以这种方式解决了这个问题:

-(void)textFieldDidBeginEditing: (UITextField *)textField{
NSLog(@"sowing keyboard");

if ((textField == timebegin)||(textField == timeend)||(textField == number)||(textField == costlist)||(textField == costnormal)||(textField == newmessagename)||(textField == noteView))     {

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}else{

}
}

-(void)textFieldDidEndEditing: (UITextField *)textField{
NSLog(@"hiding keyboard");

}

-(void)textViewDidBeginEditing: (UITextView *)textView{
NSLog(@"sowing keyboard");

if (textView == generalDetails) {

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}else{

}
}

-(void)textViewDidEndEditing: (UITextView *)textView{
NSLog(@"hiding keyboard");

}

- (void)keyboardDidHide:(NSNotification *)aNotification {

[[NSNotificationCenter defaultCenter] removeObserver:self name: UIKeyboardDidHideNotification object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self name: UIKeyboardWillShowNotification object:nil];

 NSLog(@"hiding keyboard");

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.27];
scroll1.frame = CGRectMake(0, -340, 768, 1004);
[UIView commitAnimations];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.37];
scroll2.frame = CGRectMake(0, 678, 768, 1004);
[UIView commitAnimations];

[self.mapView setHidden:NO];
    addImageForCommentingButton.hidden = NO;

//fade in
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];

[self.mapView setAlpha:1.0];
    [addImageForCommentingButton setAlpha: 1.0];

[UIView commitAnimations];

}

- (void)keyboardWillShow:(NSNotification *)notification {

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.27];
scroll1.frame = CGRectMake(0, -604, 768, 1004);
[UIView commitAnimations];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.47];
scroll2.frame = CGRectMake(0, 414, 768, 1004);
 [UIView commitAnimations];



[UIView animateWithDuration:1.0
             animations:^{
                 self.mapView.alpha=0.0;
                 [addImageForCommentingButton setAlpha: 0.0];
             }
             completion:^(BOOL finished){
                 self.mapView.hidden=YES;
                 addImageForCommentingButton.hidden = YES;
             }];
}

When I start editing one of the UItextFields whose name is in the if statement, the observer is added and the animation in the showKeyboard method runs. 当我开始编辑名称在if语句中的UItextFields之一时,将添加观察者,并且showKeyboard方法中的动画将运行。 this is not the same for the UItextView general details. 这与UItextView常规详细信息不同。 when I start editing it, the animation doesn't run, as if the code wasn't called, but it is called, and the observer is added (I understood this from NSlog). 当我开始对其进行编辑时,该动画不会运行,就像没有调用该代码一样,但是会调用该动画,并添加观察者(我从NSlog理解了这一点)。 The point is that it is the same code for UItextView and UItextField, so how can one work and the other not? 关键是,UItextView和UItextField的代码相同,那么一个如何工作而另一个却不行呢?

Solved by doing this: 通过这样做解决了:

-(void)textViewDidBeginEditing: (UITextView *)textView{
NSLog(@"sowing keyboard textView");

if (textView == generalDetails) {

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    [self keyboardWillShow];

}else{

}
}

very strange, I think that the method keyboardWillShow was not being called at all 很奇怪,我认为根本没有调用方法keyboardWillShow

If adding the [self keyboardWillShow] fixed the problem, then chances are your method was not implemented properly. 如果添加[self keyboardWillShow]解决了该问题,则可能是您的方法未正确实施。 keyboardWillShow and keyboardWillShow: are two different methods. keyboardWillShowkeyboardWillShow:是两种不同的方法。

If you want keyboardWillShow: notification to be called then it must be defined as follows: 如果要调用keyboardWillShow:通知,则必须按如下所示对其进行定义:

- (void) keyboardWillShow: (NSNotification*) notification
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM