Show Menu

Need an iOS Developer?

Submit your 30 day Job Listing for FREE

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

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 this:

2

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:


#import 
@interface ViewController : UIViewController
- (IBAction)showAlert:(UIButton *)sender;
@end

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:


#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)showAlert:(UIButton *)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Button Touched" 
                     message:@"You Touched the Button!" 
                     delegate:nil 
                     cancelButtonTitle:@"OK" 
                     otherButtonTitles:nil];
    [alert show];
}

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:

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly


Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.


Written by:

I work in the shadows. You can thank me by being a good soul.

Comments

comments