There are times when I do not have direct access to the "first responder", so I tend to use a different approach. I have a utility class for the keyboard, among other functions:
+ (BOOL)dismiss:(UIView *)view
{
if (view.isFirstResponder) {
[view resignFirstResponder];
return YES;
}
for (UIView *subView in view.subviews) {
if ([Keyboard dismiss:subView])
return YES;
}
return NO;
}
This allows me to simply call, for example: [Keyboard dismiss:self.view]from anywhere within UIViewController.
source
share