ios - Adding a simple UIAlertView


Translate

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


All Answers
  • Translate

    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> 
    

  • Translate

    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];
    

    Swift 3 / 4:

    let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
    //We add buttons to the alert controller by creating UIAlertActions:
    let actionOk = UIAlertAction(title: "OK",
        style: .default,
        handler: nil) //You can use a block here to handle a press on this button
    
    alertController.addAction(actionOk)
    
    self.present(alertController, animated: true, 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 ([UIAlertController class] == nil) { //[UIAlertController class] returns nil on iOS 7 and older. You can use whatever method you want to check that the system version is iOS 8+
    // Starting with Xcode 9, you can also use 
    // if (@available(iOS 8, *)) {
        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];
    }
    

    Swift 3 / 4:

    let alertTitle = "Title"
    let alertMessage = "Message"
    let alertOkButtonText = "Ok"
    
    if #available(iOS 8, *) {
        let alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
        //We add buttons to the alert controller by creating UIAlertActions:
        let actionOk = UIAlertAction(title: alertOkButtonText,
            style: .default,
            handler: nil) //You can use a block here to handle a press on this button
    
        alertController.addAction(actionOk)
    }
    else {
        let alertView = UIAlertView(title: alertTitle, message: alertMessage, delegate: nil, cancelButtonTitle: nil, otherButtonTitles: alertOkButtonText)
        alertView.show()
    }
    

    UPD: updated for Swift 3 / 4.

    UPD 2: added a note for @available in Objc-C starting with Xcode 9.


  • Translate

    UIAlertView is deprecated on iOS 8. Therefore, to create an alert on iOS 8 and above, it is recommended to use UIAlertController:

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Alert Message" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
    
        // Enter code here
    }];
    [alert addAction:defaultAction];
    
    // Present action where needed
    [self presentViewController:alert animated:YES completion:nil];
    

    This is how I have implemented it.


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

  • Translate
    UIAlertView *myAlert = [[UIAlertView alloc] 
                             initWithTitle:@"Title"
                             message:@"Message"
                             delegate:self
                             cancelButtonTitle:@"Cancel"
                             otherButtonTitles:@"Ok",nil];
    [myAlert show];
    

  • Translate

    As a supplementary to the two previous answers (of user "sudo rm -rf" and "Evan Mulawski"), if you don't want to do anything when your alert view is clicked, you can just allocate, show and release it. You don't have to declare the delegate protocol.


  • Translate

    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];
    }
    

  • Translate

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


  • Translate

    Simple alert with array data:

    NSString *name = [[YourArray objectAtIndex:indexPath.row ]valueForKey:@"Name"];
    
    NSString *msg = [[YourArray objectAtIndex:indexPath.row ]valueForKey:@"message"];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:name
                                                    message:msg
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    

  • Translate

    For Swift 3:

    let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
    self.present(alert, animated: true, completion: nil)