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

时间:2013-02-02 14:04:42

标签: ios objective-c xcode

我想检测某些UItextField和UItextView的编辑时间,以便运行动画。我以这种方式解决了这个问题:

-(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;
             }];
}

当我开始编辑名称在if语句中的其中一个UItextField时,将添加观察者并运行showKeyboard方法中的动画。对于UItextView的一般细节,这是不一样的。当我开始编辑它时,动画不会运行,好像代码没有被调用,但它被调用,并且添加了观察者(我从NSlog中理解这一点)。关键是它与UItextView和UItextField的代码相同,那么一个如何工作而另一个不工作呢?

2 个答案:

答案 0 :(得分:0)

解决这个问题:

-(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{

}
}

非常奇怪,我认为没有调用keyboardWillShow方法

答案 1 :(得分:0)

如果添加[self keyboardWillShow]修复了问题,那么您的方法可能无法正确实施。 keyboardWillShowkeyboardWillShow:是两种不同的方法。

如果您希望调用keyboardWillShow:通知,则必须按如下方式定义:

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