Programming iPhone Alerts

Using the Alert View

One of the views that is not listed in the XCode Library is the UIAlertView. The UIAlertView displays an alert view to the user and is usually created during runtime. Hence, to use it you have to create it using code.

The UIAlertView is useful for cases in which you have to display a message to the user. In addition, it can serve as a quick debugging tool when you want to observe the value of a variable during runtime.

The following Try It Out explores the UIAlertView in more detail. You need to download the code as indicated here.

Codefile [UsingViews.zip] available for download at Wrox.com

 

Try It Out Using the Alert View

1. Using Xcode, create a new View-based Application project and name it UsingViews.

2. In the UsingViewsViewController.m file, add the following code that appears in bold to the viewDidLoad method:

– (void)viewDidLoad {

 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"

                             message:@"This is an alert view"

                             delegate:self

                             cancelButtonTitle:@"OK"

                             otherButtonTitles:nil];

    [alert show];

    [alert release];

[super viewDidLoad];

 

}

3. Press Command-R to test the application on the iPhone Simulator. When the application is loaded, you see the alert view as shown in Figure 4-2.

Figure 4-2

4. Back in Xcode, modify the otherButtonTitles parameter by setting it with the value shown in bold:

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"

                             message:@"This is an alert view"

                             delegate:self

                             cancelButtonTitle:@"OK"

                             otherButtonTitles:@"Option 1", @"Option 2", nil];

5. In the UsingViewsViewController.h file, add the following line that appears in bold:

#import <UIKit/UIKit.h>

 

@interface UsingViewsViewController : UIViewController

    <UIAlertViewDelegate> { 

}

 

@end

6. In the UsingViewsViewController.m file, add the following method:

– (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:

    (NSInteger)buttonIndex {

 

NSLog([NSString stringWithFormat:@"%d", buttonIndex]);

 

}

7. Press Command-R to test the application in the iPhone Simulator. Notice that there are now two additional buttons besides the OK button (see Figure 4-3).

Figure 4-3

8. Click any one of the buttons — Option 1, Option 2, or OK.

9. In Xcode, press Command-Shift-R to view the Debugger Console window. Observe the values printed. You can rerun the application a number of times and click the different buttons to observe the values printed.

10. You should observe the values printed for each button clicked:

  • OK button — 0

  • Option 1 — 1

  • Option 2 — 2

How It Works

To use UIAlertView, you first instantiate it and initialize it with the various arguments:

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"

                             message:@"This is an alert view"

                             delegate:self

                             cancelButtonTitle:@"OK"

                             otherButtonTitles:nil];

The first parameter is the title of the alert view, which you set to “Hello”. The second is the message, which you set to “This is an alert view”. The third is the delegate, which you need to set to an object that will handle the events fired by the UIAlertView object. In this case, you set it to self, which means that the event handler will be implemented in the current class, that is, the View Controller. The cancelButtonTitle parameter displays a button to dismiss your alert view. Last, the otherButtonTitles parameter allows you to display additional buttons if needed. If no additional buttons are needed, simply set this to nil.

To show the alert view modally, use the show method:

    [alert show];

For simple use of the alert view, you don’t really need to handle the events fired by it. Tapping the OK button (as set in the cancelButtonTitle parameter) simply dismisses the alert view.

If you want more than one button, you need to set the otherButtonTitles parameter, like this:

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"

                             message:@"This is an alert view"

                             delegate:self

                             cancelButtonTitle:@"OK"

                             otherButtonTitles:@"Option 1", @"Option 2", nil];

Note that you need to end the otherButtonTitles parameter with a nil or a runtime error will occur.

Now that you have three buttons, you need to be able to know which button the user pressed —in particular, whether Option 1 or Option 2 was pressed. To do so, you need to handle the event raised by the UIAlertView class. You do so by ensuring that your View Controller conforms to the UIAlertViewDelegate protocol:

@interface UsingViewsViewController : UIViewController

    <UIAlertViewDelegate> {

    //…

The UIAlertViewDelegate protocol contains several methods associated with the alert view. To know which button the user tapped, you need to implement the alertView:clickedButtonAtIndex: method:

– (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:

   (NSInteger)buttonIndex {

 

    NSLog([NSString stringWithFormat:@"%d", buttonIndex]); 

}

The index of the button clicked will be passed in via the clickedButtonAtIndex: parameter. 

This article is excerpted from chapter 4 "Exploring the Views" of the book Beginning iPhone SDK Programming with Objective-C by Wei-Meng Lee (ISBN: 978-0-470-50097-2, Wrox, 2010, Copyright Wiley Publishing Inc.)

Tags:

Comments

One response to “Programming iPhone Alerts”

  1. Anonymous says:

     Hi, this is really a good book and also excellent example!

Leave a Reply to Anonymous Cancel reply

Your email address will not be published. Required fields are marked *