3

I have a UIAlertView containing a UITextField:

UITextField *textField = [alertView textFieldAtIndex:0];

Now I want to set the inputDelegate property for the textField:

[textField setInputDelegate:self];

My view controller (self) conforms to UITextInputDelegate and it has the method

- (void)textDidChange:(id<UITextInput>)textInput;

but for some reason this method is never called when I change the text in the textField. So I assume that my view controller is not set as the textField's inputDelegate.

The documentation states:

The text input system automatically assigns a delegate to this property at runtime. It is the responsibility of the view that adopts the UITextInput protocol to notify the input delegate at the appropriate junctures.

Does that mean that I cannot change the inputDelegate of a text field?

How can I achieve that the textField really calls the delegate method textDidChange: in my view controller whenever its contents change?

Mischa
  • 15,816
  • 8
  • 59
  • 117

2 Answers2

1

I have same experience that inputdelegate will never work.

I have found this page for you,

input delegate

However, to solve your problem, i suggest you use delegate instead!

UITextField *textField = [alertView textFieldAtIndex:0];

textField.delegate = self;
Community
  • 1
  • 1
kvh
  • 2,118
  • 19
  • 29
  • What you suggest is to create a new UITextField and set its delegate to self. That will not fix the problem because (1) I want to make use of the UITextField provided by the UIAlertView class and (2) I did manage to set the text field's delegate using `[textField setDelegate:self]` but I cannot set its inputDelegate. `[textField setInputDelegate:self]` does not work. – Mischa Jul 19 '13 at 14:12
  • hmm, why do you wan to use inputdelegate? to get a notification when text changed? – kvh Jul 19 '13 at 14:37
  • Yes. I know there is the text field delegate method `textField: shouldChangeCharactersInRange: replacementString:` but this method is being called _before_ the actual change of the text field's text. I need to perform the test _after_ the text has been changed. – Mischa Jul 19 '13 at 14:45
  • That is not implemented in the UIAlertView class. See above: "(1) I want to make use of the UITextField provided by the UIAlertView class" – Mischa Jul 19 '13 at 14:50
  • so, i think a customized alertview with textview in it can do it. – kvh Jul 19 '13 at 14:54
0

I don't know how to set the inputDelegate for a UITextField, but you can observe your UITextField's UITextFieldTextDidChange notification to get notified after it's text has been changed.

ma11hew28
  • 121,420
  • 116
  • 450
  • 651