Capturing User-Entered Text
Like the two long-established text box elements in HTML (<text type="input">
and <textarea>
), Cocoa Touch provides two
types of text entry controls, UITextField
and
UITextView
. The former is a
one-line text box, while the latter allows for entry of multiple text
lines in a scrollable area. In this section, you will add a UITextField
to Workbench and learn how to obtain
the text a user types into the text box.
Figure 9-8 shows
how Workbench should look when you’ve finished this section. You will add
both a UITextField
and a UILabel
so that when you finish entering text in
the box, your code copies the text to the label and clears the text box in
preparation for the next entry. To add just a little spice to the
operation, the code converts the text to initial uppercase and applies one
of three colors to the label’s text, depending on the length of the typed
text.
Figure 9-8. Workbench app equipped for UITextField demonstration
The Code Portion
Begin by adding the necessary code to the WorkbenchViewController
class files. In the
header file, insert the notation that this class implements the UITextFieldDelegate
. This will allow the text field control to send messages to the class when various conditions arise (although for this demo, you’re interested in acting only when editing ends with the press of the Return key on the keyboard). Next, add two instance variables ...
Get Learning the iOS 4 SDK for JavaScript Programmers now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.