Reputation: 5389
I've got a bit of a doubt with UITextView
s. I use to put UITextField
s, and remove the keyboard by tapping outside with the faithful old:
-(void)touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event {
for (UIView* view in self.view.subviews) {
if ([view isKindOfClass:[UITextField class]])
[view resignFirstResponder];
}
}
I have this UITextView which is set on my view (and actually is self.myTV
, to be precise), and I do the following
-(void)touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event {
for (UIView* view in self.view.subviews) {
if ([view isKindOfClass:[UITextView class]])
[view resignFirstResponder];
}
}
Same as above, save for the different class...
Well it doesn't seem to work. Anyone has a suggestion?
Upvotes: 2
Views: 1551
Reputation: 5579
An easy way to resign first responder for any subview that is currently the first responder is:
[self.view endEditing:NO];
Or if you want to force the first responder to resign without asking it first:
[self.view endEditing:YES];
Upvotes: 4
Reputation: 606
The problem is that UITextView is not FirstResponder in your case as per the subview hierarchy of your app.
I would suggest you to categorize UIView to find the first responder as under: This category on UIView, which calls on the UIWindow and traces for the first responder.
@implementation UIView (FindAndResignFirstResponder)
- (BOOL)findAndResignFirstResponder
{
if (self.isFirstResponder) {
[self resignFirstResponder];
return YES;
}
for (UIView *subView in self.subviews) {
if ([subView findAndResignFirstResponder])
return YES;
}
return NO;
}
@end
Upvotes: 2