Using UIAlertViewDelegate for iPhone

The UIAlertView class allows us to present a simple interface to alert the user that something has happened. But it also allows us to respond in different ways, depending on which button in the alert view is touched. If we want more than the default cancel button on an alert, we must adopt the UIAlertViewDelegate protocol. So let’s get started!

Start Xcode, choose “Create a new Xcode project,” select the Single View Application template, and click Next. Name the project “ColorAlert” and choose options as shown:

Click Next, choose a location to save the project, then click Create.

The first thing we’ll do is add a button to the user interface that will bring up the alert view when pressed. Open the ViewController.zip file, and drag a UIButton to the view. Change the title of the button to “Change Color…” and (if desired) change the view’s background color as well. When you are done, the interface should look something like this:

color alert

As we know, this button will require an IBAction method, so let’s open up ViewController.h and add the header for this method:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
<UIAlertViewDelegate>

(IBAction)btnTouched:(UIButton *)sender;

@end

Notice that we’ve also conformed ViewController to the UIAlertViewDelegate protocol: this is required only if we’ll be adding more than the default cancel button to the alert.

Now Open ViewController.m, and add the implementation of the btnTouched method as shown:

@implementation ViewController

(IBAction)btnTouched:(UIButton *)sender
{
    UIAlertView *colorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Change Color…"
                               message:@"Choose a Color"
                               delegate:self
                               cancelButtonTitle:@"None"
                               otherButtonTitles:@"Red", @"Yellow", @"Blue", nil];
    [colorAlert show];
}
. . .

The title will appear at the top of the alert view, with the message just below it. Next the buttons will be displayed, with the three “otherButtonTitles” buttons displayed first, followed by the “cancelButtonTitle.” The delegate parameter defines the object that will handle the button touches, in this case, “self,” meaning ViewController. Whatever object handles the button touches must conform to the UIAlertViewDelegate protocol.

Wire this method up to the Change Color… button in interface builder:

UI Alert View Delegate

If we run the application now, we’ll see the alert view when we click the button, but nothing will happen after clicking an alert button aside from the alert being dismissed. In order to handle touches on other alert buttons, we must implement one of the delegate methods of UIAlertViewDelegate: (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex. The first parameter of this method will be the alertView on which a button is touched, and the second will be the index of the touched button. Let’s implement this method in ViewController.m now:

(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 1:  //red
            self.view.backgroundColor = [UIColor redColor];
            break;
        case 2:  //yellow
            self.view.backgroundColor = [UIColor yellowColor];
            break;
        case 3:  //blue
            self.view.backgroundColor = [UIColor blueColor];
            break;
           
        default:
            break;
    }
}

The buttonIndex will be 0 if the cancel button is pressed, and incremental values starting at 1 for the otherButtonTitles array. In this case, we’re just changing the color of the ViewController’s view.

Run the application now and observe what happens as each alert button is touched:

iphone tutorials

The UIAlertView is a “modal” view (meaning that it blocks touch events from bubbling up to it’s parent view), but it doesn’t hang the UIThread, which makes it a very good control for simple choices.

Leave a Reply

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