How to create an alert box in iphone?

51,686

Solution 1

A UIAlertView is the best way to do that. It will animate into the middle of the screen, dim the background, and force the user to address it, before returning to the normal functions of your app.

You can create a UIAlertView like this:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wait" message:@"Are you sure you want to delete this.  This action cannot be undone" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil];
[alert show];

That will display the message.

Then to check whether they tapped delete or cancel, use this:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0){
        //delete it
    }
}

Make sure in your header file (.h), you include the UIAlertViewDelegate by putting <UIAlertViewDelegate>, next to whatever your class inherits from (ie. UIViewController or UITableViewController, etc.)

For more infomation on all the specifics of UIAlertViews check out Apple's Docs Here

Hope that helps

Solution 2

The post is quite old, but still a good question. With iOS 8 the answer has changed. Today you'd rather use 'UIAlertController' with a 'preferredStyle' of 'UIAlertControllerStyle.ActionSheet'.

A code like this (swift) that is bound to a button:

@IBAction func resetClicked(sender: AnyObject) {
    let alert = UIAlertController(
        title: "Reset GameCenter Achievements",
        message: "Highscores and the Leaderboard are not affected.\nCannot be undone",
        preferredStyle: UIAlertControllerStyle.ActionSheet)
    alert.addAction(
        UIAlertAction(
            title: "Reset Achievements",
            style: UIAlertActionStyle.Destructive,
            handler: {
                (action: UIAlertAction!) -> Void in
                gameCenter.resetAchievements()
            }
        )
    )
    alert.addAction(
        UIAlertAction(
            title: "Show GameCenter",
            style: UIAlertActionStyle.Default,
            handler: {
                (action: UIAlertAction!) -> Void in
                self.gameCenterButtonClicked()
            }
        )
    )
    alert.addAction(
        UIAlertAction(
            title: "Cancel",
            style: UIAlertActionStyle.Cancel,
            handler: nil
        )
    )
    if let popoverController = alert.popoverPresentationController {
        popoverController.sourceView = sender as UIView
        popoverController.sourceRect = sender.bounds
    }
    self.presentViewController(alert, animated: true, completion: nil)
}

would produce this output: enter image description here

EDIT: The code crashed on iPad, iOS 8+. If added the necessary lines as described here: on another stack overflow answer

Solution 3

For swift it is very simple.

    //Creating the alert controller
    //It takes the title and the alert message and prefferred style
    let alertController = UIAlertController(title: "Hello  Coders", message: "Visit www.simplifiedios.net to learn xcode", preferredStyle: .Alert)

    //then we create a default action for the alert... 
    //It is actually a button and we have given the button text style and handler
    //currently handler is nil as we are not specifying any handler
    let defaultAction = UIAlertAction(title: "Close Alert", style: .Default, handler: nil)

    //now we are adding the default action to our alertcontroller
    alertController.addAction(defaultAction)

    //and finally presenting our alert using this method
    presentViewController(alertController, animated: true, completion: nil)

Ref: iOS Show Alert Message

Solution 4

Everyone's saying UIAlertView. But to confirm deletion, UIActionSheet is likely the better choice. See When to use a UIAlertView vs. UIActionSheet

Share:
51,686
Blane Townsend
Author by

Blane Townsend

Updated on May 18, 2020

Comments

  • Blane Townsend
    Blane Townsend about 4 years

    I would like to make an alert type box so that when the user tries to delete something, it says, "are you sure" and then has a yes or no for if they are sure. What would be the best way to do this in iphone?