iosobjective-cuikeyboardnsnotification

NSNotification keyBoardWillShow and keyboardWillHide are not called


I have a xib file with custom view on which I have many textfields subviews. I have set delegates on each of the textfield to the file owner and successfully able to texfield delegate methods. But unfortunately the keyboard notification methods keyboardWillShow: and keyboardWillHide: not being called at all.

I added observers at textfieldShouldBeginEditing and removed the observers at textFieldDidEndEditing.

Here is the snippet of my code:

Adding Observer

-(void) textFieldShouldBeginEditing : (UITextField *) textField{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}

Removing observer

-(void) textFieldDidEndEditing : (UITextField *) textField{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

//Methods for notification

- (void)keyboardWillShow:(NSNotification *)notification
{
NSLog(@"Here");
}

- (void)keyboardWillHide:(NSNotification *)notification
{
NSLog(@"There");
}

//Any of those keyboardwillShow and keyboardWillHide is not called.

It will be great if someone can help me figure out the issue with my code or any underlaying issue which I may be missing.


Solution

  • You are adding an observer when you are showing keyboard by editing textView. It won't call because keyboard is already shown. You should add observers on your viewWillAppear and remove viewWillDisappear methods.