Find View that is the firstresponder

Armand picture Armand · Mar 26, 2012 · Viewed 13.1k times · Source

I would like to get the view that is the first responder, currently I have a UITableView that contains UITextFields, using a method:

-(UIView*) findFirstResponder
{

}

I would like to be able to get the view that is the firstResponder and then do something with that view.

Any ideas?

Answer

Armand picture Armand · Mar 26, 2012

All I had to do was

@implementation UIView (FindViewThatIsFirstResponder)
- (UIView *)findViewThatIsFirstResponder
{
    if (self.isFirstResponder) {        
        return self;     
    }

    for (UIView *subView in self.subviews) {
        UIView *firstResponder = [subView findViewThatIsFirstResponder];
        if (firstResponder != nil) {
            return firstResponder;
        }
    }

    return nil;
}
@end