Reputation: 77661
I have an observer for UIKeyboardWillShowNotification
and UIKeyboardWillHideNotification
.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
It all works, except it works when the viewController is not currently visible.
I've tried comparing to self.navigationcontroller.topViewController
but this doesn't work when I have a modal view presented as the topViewController
is the one underneath the modal view controller.
Upvotes: 6
Views: 7821
Reputation: 6432
If you're using UIViewController
you could register your instance for Keyboard Notifications when the view becomes visible inside viewWillAppear:
and then de-register when the view gets hidden inside viewWillDisappear:
This way you won't receive notifications when the view is not visible.
Hope this helps!
Upvotes: 7
Reputation: 2636
If you only want to react to that notification when the viewController is visible, then just check if is visible at the beginning of the function:
- (void)keyboardWillShow:(NSNotification *)notification
{
if ([self.view window]) //means is visible
//do something
else
//return
}
Upvotes: 3