Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 816
Armand Avatar asked Mar 26 '12 15:03

Armand


2 Answers

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
like image 153
Armand Avatar answered Oct 17 '22 08:10

Armand


Use UIControl as a root reference to different types of control that can become first responder.

UIControl *currentControl;

As Gobot says - whenever a textfield becomes first responder, keep a note of which one it is...

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {   

    currentControl = textField;

    . . .
like image 24
Damo Avatar answered Oct 17 '22 08:10

Damo