Search code examples
iphoneiosfirst-responder

Find View that is the firstresponder


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?


Solution

  • 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