Search
Mobile

iPhone Application Development : Basic User Input and Output

Published 2 min readBy Programming4us Editorial Team
Buttons

One of the most common interactions you’ll have with your users is detecting and reacting to the touch of a button (UIButton). Buttons, as you may recall, are elements of a view that respond to an event that the user triggers in the interface, usually a Touch Up Inside event to indicate that the user’s finger was on a button and then released it. Once an event is detected, it can trigger an action (IBAction) within a corresponding view controller.

Buttons are used for everything from providing preset answers to questions to triggering motions within a game. Although we’ve used only a single Rounded Rect button up to this point, they can take on many different forms through the use of images. Figure 1 shows an example of a fancy button.

Figure 1. Buttons can be simple, fancy (like this one), or set to any arbitrary image.


Text Fields and Views

Another common input mechanism is a text field. Text fields (UITextField) give users space to enter any information they’d like into a single line in the application—these are similar to the form fields in a web form. When users enter data into a field, you can constrain their input to numbers or text by using different iPhone keyboards, something we do later this hour. Text fields, like buttons, can respond to events but frequently are implemented as passive interface elements, meaning that their contents (provided through the text property) can be read at any time by the view controller.

Similar to the text field is the text view (UITextView). The difference is a text view can present a scrollable and editable block of text for the user to either read or modify. These should be used in cases where more than a few words of input are required. Figure 7.2 shows examples of a text field and text view.

Figure 7.2. Text fields and text views provide a means for entering text using the iPhone’s virtual keyboard.


Labels

The final interface feature that we’re going to be using here and throughout this book is the label (UILabel). Labels are used to display strings within a view by setting their text property.

The text within a label can be controlled via a wide range of label attributes, such as font and text size, alignment, and color. As you’ll see, labels are useful both for static text in a view and for presenting dynamic output that you generate in your code.

Now that you have basic insight into the input and output tools we’ll be using in this hour, let’s go ahead and get started with our project: a simple substitution-style story generator.