13

I have set a custom inputView to my UITextField. I need a way to display the data selected in my custom inputView in the UITextfield. I would like to achieve this the same way the system keyboard does it.

Does anyone know how this is done? How does the system keyboard get a reference to the UITextfield that is the first responder?

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
Darren Findlay
  • 2,533
  • 2
  • 29
  • 46
  • please be more precise, what do you mean by "direct input back to the textfield"? You want to focus the UITextField? – Dion Apr 10 '12 at 21:53
  • Sorry, i thought i was more precise in my explanation. What i mean is, i set the inputView property on a UITextField to my custom input view. So when the user pushes a button on my custom inputView, how do i get a reference to the UITextfield that is the current first responder so i can change the text property of that UITextfield to reflect the user's input? – Darren Findlay Apr 10 '12 at 21:59

2 Answers2

15

When sending input from your custom input view back to the textfield, note that you can (should) use the UIKeyInput protocol methods that UITextField conforms to. Namely:

- (void)deleteBackward;
- (void)insertText:(NSString *)text;
Joshua J. McKinnon
  • 1,696
  • 1
  • 18
  • 29
13

How does the system keyboard get a reference to the UITextfield that is the first responder?

It just asks the system for the first responder; unfortunately, that's a private UIKit method (or was, last I checked). You can find the first responder by recursing through the view hierarchy and asking each view, but that's pretty clumsy.

Instead, you can add a link to the text field on the input view (I'm assuming your input view is a custom UIView subclass):

@property(nonatomic, assign) UITextField* target;

Then use the UITextField delegate methods in your view controller to see when the text field is being focused:

- (void)textFieldDidBeginEditing:(UITextField*)textField
{
    if ( [textField.inputView isKindOfClass:[MyInputView class]] )
        ((MyInputView*)textField.inputView).target = textField;
}

- (void)textFieldDidEndEditing:(UITextField*)textField
{
    if ( [textField.inputView isKindOfClass:[MyInputView class]] )
        ((MyInputView*)textField.inputView).target = nil;
}
davehayden
  • 3,484
  • 21
  • 28