簡體   English   中英

編輯UItextview時隱藏並顯示IOS鍵盤

[英]Hide and show IOS Keyboard when editing UItextview

我想檢測何時編輯某些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語句中的UItextFields之一時,將添加觀察者,並且showKeyboard方法中的動畫將運行。 這與UItextView常規詳細信息不同。 當我開始對其進行編輯時,該動畫不會運行,就像沒有調用該代碼一樣,但是會調用該動畫,並添加觀察者(我從NSlog理解了這一點)。 關鍵是,UItextView和UItextField的代碼相同,那么一個如何工作而另一個卻不行呢?

通過這樣做解決了:

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

如果添加[self keyboardWillShow]解決了該問題,則可能是您的方法未正確實施。 keyboardWillShowkeyboardWillShow:是兩種不同的方法。

如果要調用keyboardWillShow:通知,則必須按如下所示對其進行定義:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM