29

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?

Andrew
  • 3,874
  • 5
  • 39
  • 67
Blane Townsend
  • 2,888
  • 5
  • 41
  • 55
  • @一二三: You have over 1000 rep; isn't there a 'retag' link that you can use to retag questions without going through approval if you're not editing the question content? – BoltClock May 03 '11 at 05:08

10 Answers10

62

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

starball
  • 20,030
  • 7
  • 43
  • 238
Andrew
  • 3,874
  • 5
  • 39
  • 67
14

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

Community
  • 1
  • 1
jboi
  • 11,324
  • 4
  • 36
  • 43
  • Could you add an example of each different style maybe? I know the user is asking for deletion but it'd still be nice to see the other types of UIAlertControllesr (aka the good old UIAlertView that everyone is talking about :D) – Gil Sand Jan 08 '15 at 15:23
  • Hi @Zil no problem. I've added the third action style (Default style). I've no experience with other preferred styles, but you can easily take the code snippet an try it out. If you can find out which App on the store this comes from, you get an invitation as beta tester ;-) – jboi Jan 09 '15 at 08:35
2

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

Shaba Aafreen
  • 1,497
  • 2
  • 8
  • 4
1

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

Community
  • 1
  • 1
Jon Reid
  • 20,545
  • 2
  • 64
  • 95
0

Being that UIAlertView is now deprecated, I wanted to provide an answer to future coders that come across this.

Instead of UIAlertView, I would use UIAlertController like so:

@IBAction func showAlert(_ sender: Any) {
  let alert = UIAlertController(title: "My Alert", message: "This is my alert", preferredStyle: UIAlertControllerStyle.alert)

  alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: {(action) in 
    alert.dismiss(animated: true, completion: nil)
  }))
  self.present(alert,animated:true, completion:nil)
}
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
danner.tech
  • 109
  • 2
  • 13
0

UIAlertView seems the obvious choice for confirmation.

Set the delegate to the controller and implement the UIAlertViewDelegate protocol http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html

InsertWittyName
  • 3,930
  • 1
  • 22
  • 27
0

Use the UIAlertView class to display an alert message to the user.

Black Frog
  • 11,595
  • 1
  • 35
  • 66
0

Use an UIAlertView:

UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Alert Title" 
                                                     message:@"are you sure?"
                                                    delegate:self 
                                           cancelButtonTitle:@"No"
                                           otherButtonTitles:@"Yes", nil];


        [av show];
        [av autorelease];

Make sure you implement:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

To handle the response.

Richard Stelling
  • 25,607
  • 27
  • 108
  • 188
0

To pop an alert message use UIAlertView.

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

Once you set the delegate as self you can perform your action on this method

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Anand
  • 2,086
  • 2
  • 26
  • 44
-1

Here i provided the alert message, which i used in my first app:

@IBAction func showMessage(sender: UIButton) {
    let alertController = UIAlertController(title: "Welcome to My First App",
                              message: "Hello World",
                              preferredStyle: UIAlertControllerStyle.alert)
    alertController.addAction(UIAlertAction(title: "OK",
                                            style: UIAlertActionStyle.default,
                                            handler: nil))
    present(alertController, animated: true, completion: nil)
}

with appropriate handlers for user responses.

user8472
  • 3,268
  • 3
  • 35
  • 62
Pallavi
  • 11
  • 2