Chapter 7

Communicating with Your Users

The user interface elements you have encountered so far have all been created by dragging and dropping from the Object library. In this lesson you are introduced to UIAlertView and UIActionSheet, two user interface elements that are created only with code.

Alert Views

An alert view is a special modal view that is used to display a short message to the user and typically enables the user to choose from a small number of options. The most common use of an alert view is to display information on success or failure of an operation; for example, on success a typical login operation may display an alert view, as shown in Figure 7-1.

When an alert view is displayed, the screen is dimmed automatically for you. You can specify a title, a message, and one or more buttons to present the user with options. One of these buttons is always designated as the cancel button, and though you can change the text displayed in it, it is always displayed at the bottom of the alert view with a small offset from the other buttons, as shown in Figure 7-2.

An alert view is an instance of the UIAlertView class, which is part of the UIKit framework, and is created in code as follows:

UIAlertView* message = [[UIAlertView alloc] 
                       initWithTitle:@"This is the title" 
                       message:@"This is the message text" 
                       cancelButtonTitle:@"Cancel Button" 
                       otherButtonTitles:@"Option 1", @"Option 2", nil];

The first parameter is the title of the alert view. This is followed by the ...

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