1

In my iOS app I have several UIElements that can process user input: textfields, editable webviews, etc. each time I write something into these UIElements the keyboard (obviously) will come up. Before it happens I can catch this event by observing the UIKeyboardWillShowNotification.

I would like to know what's the way to find out which UIElement invoked this action.

Thanks for your help!

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Zoltan Varadi
  • 2,468
  • 2
  • 34
  • 51

2 Answers2

3

The keyboard is launched when the view tapped by a user is set as FirstResponder, so I think this question is the equivalent of saying how do I get the current first responder when UIKeyboardWillShowNotification is received?.

The answer to that question by Thomas Muller was to use a class extension along the lines of:

@implementation UIView (FindFirstResponder)
- (UIView *)findFirstResponder
{
    if (self.isFirstResponder) {        
        return self;     
    }

    for (UIView *subView in self.subviews) {
        UIView *firstResponder = [subView findFirstResponder];

        if (firstResponder != nil) {
            return firstResponder;
        }
    }

    return nil;
}
@end

So I think you could use that inside your handler for UIKeyboardWillShow to figure out what caused it.

Community
  • 1
  • 1
Myk Willis
  • 12,306
  • 4
  • 45
  • 62
-1

I think the section 4 (Moving Content That Is Located Under the Keyboard) of this document can give you a hint about knowing wich element has the keyboard.

http://developer.apple.com/library/ios/#DOCUMENTATION/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW1

LFCPirex
  • 79
  • 9