Many times, we want to give a visual indication to the user that something has happened. We might tell the user that a file was saved, information has loaded, or some condition has been met that they should be informed of. We might elect to do this with a UILabel, but for a truly attention-getting alert, nothing beats a UIAlertView. In this blog, we’ll see how to show an alert. Let’s get started!
Open up Xcode, choose “Create a new Xcode project.” Select the Single View Application template, and click Next. Name your project AlertDemo, and choose options as shown below:
Click Next, choose a location to save the project, and Click create.
After the project has been generated, select the ViewController.zip file, and place a single UIButton on the view. Change it’s title property to “Press Me.” Your .zib file should look something like the image to the right.
We will wire up this button to an IBAction method in the ViewController that will create and show a UIAlertView. Before we do that, we should talk about the UIAlertView’s initialization method.
The most often used method to initialize an alert view is
initWithTitle:(NSString *) message:(NSString *) delegate:(id) cancelButtonTitle:(NSString *) otherButtonTitles:(NSString *), ..., nil
We always provide a title and a message, as well as a cancel button title in this method. The delegate argument is most often left nil, but may point to a protocol that can be used by the alert view. Other Button Titles can be used, and the alert view will display them, but action methods must be provided to these other buttons in order for them to work. If we only want a cancel button (which will automatically dismiss the alert view), then we should leave the otherButtonTitles argument nil as well.
Note also that the last argument must be nil as well; this is because the otherButtonTitles argument is an NSArray of NSString, and NSArrays must be nil terminated. If we set otherButtonTitles to nil, the final nil is of course not needed.
Open up ViewController.h and make the following change to the code:
@interface ViewController : UIViewController
- (IBAction)showAlert:(UIButton *)sender;
We’ve added an IBAction method declaration to the header file. This method will be called when the user presses the button. Wire this method up to the UIButton’s Touch Up Inside event in Interface Builder now.
Open up ViewController.m, and make the changes shown here:
@interface ViewController ()
- (IBAction)showAlert:(UIButton *)sender
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Button Touched"
message:@"You Touched the Button!"
The only change we’ve made to this file is that we’ve added the implementation of showAlert. The UIAlertView is instantiated as alert, and in its initialization, we’ve provided a title, message, and the text that will appear on the cancel button. After allocating and initializing the alert view, we display it by sending it the “show” message.
Running the program gives the following output:
There are many more features of alert views than can be discussed in this blog. For more information, consult the UIAlertView class reference, available both through the organizer in Xcode and at developer.apple.com.