Tutorial details

Show alert (UIAlertView) on iPhone | App Code for Sale | Preview

Show alert (UIAlertView) on iPhone | iOS Tutorial

How to give a visual indication to the user that something has happened using UILabel.

Overview PAGE TOP

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:

Show_alert_(UIAlertView)_on_iPhone1.jpg

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:

227.png

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 <UIKit/UIKit.h>
@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:

Show_alert_(UIAlertView)_on_iPhone3.jpg

Reference PAGE TOP

Show alert (UIAlertView) on iPhone from http://ios-blog.co.uk

This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

1 Comments Leave a comment

Please login in order to leave a comment.

Newest first
  • madhavajay 2013-06-18 07:01:20 Thread #41

    The equivalent on Android would be the Android AlertDialog.

    We just released: PortKit: UX Metaphor Equivalents for iOS & Android

    http://kintek.com.au/blog/portkit-ux-metaphor-equivalents-for-ios-and-android/

    It has side by side comparisons of the native ui widgets and links to downloadable PSDs for designing.

  • madhavajay 2013-06-18 07:01:20

    The equivalent on Android would be the Android AlertDialog.

    We just released: PortKit: UX Metaphor Equivalents for iOS & Android

    http://kintek.com.au/blog/portkit-ux-metaphor-equivalents-for-ios-and-android/

    It has side by side comparisons of the native ui widgets and links to downloadable PSDs for designing.

!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.