1

Why does -[UIKeyInput hasText] always return NO for UITextField? Is this a bug with UIKit?

For example, the following test fails:

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectZero];
textField.text = @"has text";
STAssertTrue([textField hasText], @"any text is text.");
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • Could you add more details? Your code maybe? – Fabián Heredia Montiel Dec 11 '11 at 17:05
  • Did some testing and it seams to return `NO` until you give the text field focus, weird. – Mattias Wadman Dec 11 '11 at 17:40
  • I would guess that the `UIKeyInput` protocol somehow has more to do with the text input system than the text field content. For example a text field seams to be unaffected by calls to `insertText:` unless it's the first responder. If you look in the documentation is actually says "YES if the **backing store** has textual content, NO otherwise.". – Mattias Wadman Dec 11 '11 at 17:52

1 Answers1

4

UITextField implements UIKeyInput through the UITextInput Protocol, which states:

UIKeyInput protocol—Implemented to acquire the capabilities of text entry and deletion at an insertion point.

(via: http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UITextInput_Protocol/Reference/Reference.html)

As Mattias suggested above, it has more to do with the backing store, and is meant mainly for input through the system keyboard.

In your example, you are manipulating the property directly and circumventing input through the keyboard. By giving the field first responder, the keyboard will be shown and hasText will return YES as expected.

jverdi
  • 1,506
  • 13
  • 14