Adding a simple UIAlertView

| | August 8, 2015

What is some starter code I could use to make a simple UIAlertView with one “OK” button on it?

7 Responses to “Adding a simple UIAlertView”

  1. This page shows how to add UIAlertController if you are using Swift

  2. Other answers already provide information for iOS 7 and older, however UIAlertView is deprecated in iOS 8.

    In iOS 8+ you should use UIAlertController. It is a replacement for both UIAlertView and UIActionSheet. Documentation: UIAlertController Class Reference. And a nice article on NSHipster.

    To create a simple Alert View you can do the following:

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title"
                                                                             message:@"Message"
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    //We add buttons to the alert controller by creating UIAlertActions:
    UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
                                                       style:UIAlertActionStyleDefault
                                                     handler:nil]; //You can use a block here to handle a press on this button
    [alertController addAction:actionOk];
    [self presentViewController:alertController animated:YES completion:nil];
    

    Note, that, since it was added in iOS 8, this code won’t work on iOS 7 and older. So, sadly, for now we have to use version checks like so:

    NSString *alertTitle = @"Title";
    NSString *alertMessage = @"Message";
    NSString *alertOkButtonText = @"Ok";
    
    if ([MyOwnUtilsClass iOSMajorVersion] < 8) { //You can use whatever method you want to check the system version.
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:alertTitle
                                                            message:alertMessage
                                                           delegate:nil
                                                  cancelButtonTitle:nil
                                                  otherButtonTitles:alertOkButtonText, nil];
        [alertView show];
    }
    else {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle
                                                                                 message:alertMessage
                                                                          preferredStyle:UIAlertControllerStyleAlert];
        //We add buttons to the alert controller by creating UIAlertActions:
        UIAlertAction *actionOk = [UIAlertAction actionWithTitle:alertOkButtonText
                                                           style:UIAlertActionStyleDefault
                                                         handler:nil]; //You can use a block here to handle a press on this button
        [alertController addAction:actionOk];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
  3. Here is a complete method that only has one button, an ‘ok’, to close the UIAlert:

        - (void) myAlert: (NSString*)errorMessage
        {
            UIAlertView *myAlert = [[UIAlertView alloc]
                                  initWithTitle:errorMessage
                                  message:@""
                                  delegate:self
                                  cancelButtonTitle:nil
                                  otherButtonTitles:@"ok", nil];
            myAlert.cancelButtonIndex = -1;
            [myAlert setTag:1000];
            [myAlert show];
        }
    
  4. When you want the alert to show, do this:

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ROFL" 
                                                        message:@"Dee dee doo doo." 
                                                        delegate:self 
                                                        cancelButtonTitle:@"OK" 
                                                        otherButtonTitles:nil];
        [alert show];
    
        // If you're not using ARC, you will need to release the alert view.
        // [alert release];
    

    If you want to do something when the button is clicked, implement this delegate method:

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
        // the user clicked OK
        if (buttonIndex == 0) {
            // do something here...
        }
    }
    

    And make sure your delegate conforms to UIAlertViewDelegate protocol:

    @interface YourViewController : UIViewController <UIAlertViewDelegate> 
    
  5. Brynner Ferreira on November 30, -0001 @ 12:00 AM
    UIAlertView *myAlert = [[UIAlertView alloc] 
                             initWithTitle:@"Title"
                             message:@"Message"
                             delegate:self
                             cancelButtonTitle:@"Cancel"
                             otherButtonTitles:@"Ok",nil];
    [myAlert show];
    
  6. As a supplementary to the two answers above, if you don’t want to do anything when your alert view is clicked, you can just alloc and show and release it, you don’t have to declare the delegate protocol.

  7. UIAlertView *alert = [[UIAlertView alloc]
     initWithTitle:@"Title" 
     message:@"Message" 
     delegate:nil //or self
     cancelButtonTitle:@"OK"
     otherButtonTitles:nil];
    
     [alert show];
     [alert autorelease];
    

Leave a Reply