iOS 7 Views - Alert View

Alert views display an informative alert message to the user. Alert views interrupts the user and requires them to stop what they’re doing to choose an action or dismiss the alert. In this tutorial we will display an alert view when the user clicks a button.

Update: June 25, 2014. This tutorial is made with Xcode 5 and targeted for iOS 7. The rewritten version in Swift for iOS8 and Xcode 6 is available here.

Open Xcode and create a new Single View Application. For product name, use iOS7AlertViewTutorial and then fill out the Organization Name, Company Identifier and Class Prefix fields with your customary values. Make sure only iPhone is selected in Devices.

Go to the Storyboard and drag a button to the main view. Give the button a title of "Show Alert".

Select the Assistant Editor and open ViewController.m. Ctrl and drag from the button to the @interface section and create the  following Action

Implement the showAlert method

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert View Tutorial" message:@"Button is clicked." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alertView show];

Build an Run this project, select the button and an alert view will be displayed.

You can download the source code of the iOS7AlertViewTutorial at the ioscreator repository on github.