Lesson 11

Handling User Input

In Lesson 9 you were introduced to the UIButton and UILabel classes. The UILabel class enables 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 storyboard scene (see Figure 11.1).

Screenshot of a storyboard screen with Text Field object in the storyboard scene and Text Field under Object library is encircled with an arrow pointing to the Text Field object.

Figure 11.1

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

Screenshot of Attribute Inspector dialog box with Default highlighted in drop-down menu of Keyboard type.

Figure 11.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 Attribute ...

Get Swift iOS 24-Hour Trainer now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.