KeyboardWillShow not responding

here is the problem, when I debug my program, I found that the keyboardWillShow function does not respond every time. for the first time it will be called by the program. here is my code, I don’t know what is wrong in my code, but when the keyboard first appeared, the function works well.

- (void)keyboardWillShow:(NSNotification *)notification { /* Reduce the size of the text view so that it not obscured by the keyboard. Animate the resize so that it in sync with the appearance of the keyboard. */ NSDictionary *userInfo = [notification userInfo]; // Get the origin of the keyboard when it displayed. NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; // Get the top of the keyboard as the y coordinate of its origin in self view coordinate system. The bottom of the text view frame should align with the top of the keyboard final position. CGRect keyboardRect = [aValue CGRectValue]; keyboardRect = [self.view convertRect:keyboardRect fromView:nil]; CGFloat keyboardTop = keyboardRect.origin.y; CGRect newTextViewFrame = self.textview.frame; newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y; // Get the duration of the animation. NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; NSTimeInterval animationDuration; [animationDurationValue getValue:&animationDuration]; // Animate the resize of the text view frame in sync with the keyboard appearance. [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:animationDuration]; textview.frame = newTextViewFrame; [UIView commitAnimations]; } - (void)keyboardWillHide:(NSNotification *)notification { NSDictionary* userInfo = [notification userInfo]; /* Restore the size of the text view (fill self view). Animate the resize so that it in sync with the disappearance of the keyboard. */ NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; NSTimeInterval animationDuration; [animationDurationValue getValue:&animationDuration]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:animationDuration]; // textview.frame = self.view.bounds; [self save]; [UIView commitAnimations]; } 

and I register a notice

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } 

and delete it here

 - (void)viewDidUnload { [super viewDidUnload]; [self save]; self.textview = nil; self.title = nil; self.tags = nil; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } 

i resigns firstresponder, here is my code

 - (IBAction)save:(id)sender { self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNote)] autorelease]; [textview resignFirstResponder]; } - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(save:)] autorelease]; [self setUpUndoManager]; return YES; } - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{ [self save]; return YES; } 
+6
source share
1 answer

Make sure you do not write any code to remove the observer ..... Please also suggest the keyboardWillHide method.

+5
source

Source: https://habr.com/ru/post/896536/


All Articles