Chapter 6

Handling User Input

In Lesson 4 you were introduced to the UILabel class that enabled you to display static text on the screen. In this lesson you learn to use text fields and text views to accept input from users. Text fields enable users to type a single line of text and are instances of the UITextField class. Text views, on the other hand, enable users to type in multiple lines of text and are instances of the UITextView class. Both classes are part of the UIKit framework.

Text Fields

To create a text field, simply drag and drop a Text Field object from the Object library onto a view controller (Figure 6-1).

You can use the Attributes inspector to set up several attributes of the text field including the Placeholder, Alignment, Border Style, Text Color, Font Attributes, and the type of keyboard that is displayed when the user taps on the text field (Figure 6-2).

A placeholder is some text that is displayed in the text field when it is empty, typically prompting the user to enter some information in the field. You can choose from seven different keyboards to associate with a text field; the choice you make will depend on the type of data you expect. These keyboard styles can be selected using the Attributes inspector and are displayed in Figure 6-3.

The text displayed in a text field is an instance of an NSString object. The NSString class is defined in the Foundation framework and its instances represent sequences of characters (alphabets, numbers, punctuations) known ...

Get iPhone and iPad App 24-Hour Trainer 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.