I had implemented this:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
Because I have an scroller and other things. But the problem is that the iPad has a button in the keyboard to hide the keyboard, but it doesn't stop editing (the flashing line remains)
I want that if he hides the keyboards, the text resign itself after the time of keyboard animation. This works fine.
BUT If I rotate, the method is called and the textField resigns itself (Is called not even once, but three times)
How do I stop UIKeyboardWillHideNotification
to fire when the UI rotates?
If that is not the way, how do I properly resign responder when that button is tapped?
Here is my code:
float keyboardHeightSize;
- (void)keyboardWillShow:(NSNotification*)notification {
keyboardHeightSize = [[[notification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size.height;
[_scroller setContentSize:CGSizeMake(0, 1954+keyboardHeightSize)];
}
- (void)keyboardWillHide:(NSNotification*)notification {
[_scroller setContentSize:(CGSizeMake(0, 1954))];
CGFloat keyboardAnimationDuration = [[[notification userInfo]objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
[self performSelector:@selector(resignResponders) withObject:nil afterDelay:keyboardAnimationDuration];
}
-(void)resignResponders {
for (id textField in self.scroller.subviews)
{
if ([textField isKindOfClass:[UITextField class]])
{
UITextField *theTextField = textField;
if ([theTextField isFirstResponder]) {
[theTextField resignFirstResponder];
}
}
}
}