简体   繁体   中英

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. 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. this is not the same for the UItextView general details. 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). The point is that it is the same code for UItextView and UItextField, so how can one work and the other not?

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

If adding the [self keyboardWillShow] fixed the problem, then chances are your method was not implemented properly. keyboardWillShow and keyboardWillShow: are two different methods.

If you want keyboardWillShow: notification to be called then it must be defined as follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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