Hassy
Hassy

Reputation: 5208

iOS - UIKeyboardWillShowNotification called multiple Times when lock screen

I am creating a notification on viewdidload UIKeyboardWillShowNotification and removing it on viewwilldisappear.

When the keyboard appear on screen and its notification is called once as expected, I locked the screen. The notification is called 4 times and creating undesired functionality.

Why the notification being called? how can i prevent from this scenario?

I am adding Observer for notification in viewdidload in this manner

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

EDIT: Same results if i send the app to background instead of lock screen.

Upvotes: 1

Views: 1088

Answers (2)

balkaran singh
balkaran singh

Reputation: 2786

that same thing was happend with me

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

i was presenting a view controller on button click. every time i present Viewcontroller viewdidload get called it add the addObserver. so the method get called multiple time.

Upvotes: 2

In your case Notification calls multiple times that shows the addObserver calls multiple time or you are not removing observer once its usage is done.

Simply follow this things :

  • As you mentioned you are removing observer in viewwilldisappear method. So try to debug & see if it gets called. If this method not getting called try to remove it in viewDidUnload method.
  • If still you are not able to remove the observer than just simply put remove observer code before the line of add observer. That's it. It will work for sure.

Note :

  • Solution i have suggested to you is already tried & tested & works fine on my end.

Hope it will help you.

Upvotes: 0

Related Questions