UIKeyboardWillShowNotification is erroneously called from the next class on the stack

I detect when the keyboard will show the code below. However, when I click on another screen using pushViewController and open the keyboard on this screen, the WillShow keyboard gets a call! Is this really right?

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; // register for keyboard notifications [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 
+4
source share
1 answer

Yes, this is the correct behavior. Because a view that pushes another view is still lively, and notifications are widely used in the application.

You can delete the notification in:

 - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } 

And if you want to set the observer, then put the code from viewDidLoad into viewWillAppear: (BOOL) animated:

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; // register for keyboard notifications [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } 
+14
source

All Articles