How to create an alert box in iphone?

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?

-------------Problems Reply------------

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

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: How to create an alert box in iphone?

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

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

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

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

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.

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

Category:iphone Views:2 Time:2011-05-02

Related post

  • Modal Alert box for iphone using Jqtouch 2010-08-24

    I am developing a simple iPhone web application. I am stuck with creating an alert box using jqtouch. Does anybody how to create a sample modal alert box on the iPhone? I don't want to use the existing JavaScript alert due the title bar problem. Are

  • Creating Custom Alert box in android 2010-10-13

    I know this is been answered many times but i am unable to find any good resource for this. Can any one tell how to create Custom Alert box for android??. What i want is a nice looking alert box with few images text etc which will be basically mu app

  • Dynamically create bootstrap alerts box through javascript 2012-04-10

    I'm using bootstrap 2.0 for my project and I would like to dynamically add bootstrap alert box in my page (http://twitter.github.com/bootstrap/javascript.html#alerts). I want to do something like bootstrap-alert.warning("Invalid Credentials"); ------

  • How can I create an Alert Box in a PHP file? 2011-02-08

    I need a simple ok alert box in my php file. --------------Solutions------------- Not possible in PHP, because it runs on a server. The alert box is a client side feature. You must use Javascript. Try that: echo '<script type="text/javascript">

  • Create a tablayout in an alert box 2014-02-20

    In my application, I want to create an alert box with tab layouts. Is it possible to create a tab Layout in a alert box?? Please help me out.Give a proper solution. Thanks in advance !! --------------Solutions------------- you can create a xml with t

  • Alert Boxes Adobe Flex mobile - Searching - Block until get response 2011-03-29

    Im creating an application on Adobe Flex mobile (Blackberry playbook to be exact). Im designing an app that has a search function that makes a simple HTTP request to a server and waits for a response... When the user clicks on search, how do I create

  • How to display an Alert box without cancel and ok button? 2011-04-05

    I like to create an Alert box without ok and cancel button. The alert box should display for a particular period of time. --------------Solutions------------- http://www.androidpeople.com/android-custom-dialog-example remove the button in the example

  • Force Close trying to launch a market page from and alert box 2011-04-11

    Can anyone figure out why this causes a force close?? void failbox(){ // Create the alert box AlertDialog.Builder alertbox = new AlertDialog.Builder(this); // Set the message to display alertbox.setMessage(R.string.fail); alertbox.setPositiveButton("

  • How do I negotiate alert boxes in Watir? 2011-05-17

    I am trying to create an automation script for a website using Ruby with Watir. I ran into a situation where I have to choose 'Yes' or 'No' in an alert box. The problem is that I am not able to identify the alert box as a component of the page so tha

  • help needed for combo box in iphone 2011-05-30

    Please help me to create drop down box using iphone. I tried in pickerview with textfield but not worked as my requirement can u suggest the url for to solve this problem. The data should come from xml and as a select option in html. --------------So

  • How to create bubbble message box similar to iPhone buildin SMS app? 2009-06-08

    How to create bubbble message box similar to iPhone buildin SMS app? I'm trying to create a table cells with a bubble message box, similar to the iPhone SMS app or iChat. Anybody have example code I can refer to? --------------Solutions-------------

  • iPhone SDK: How can you hide an alert box? 2011-01-28

    I am using the following code to show an alert box: UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Info" message:@"My Message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; Can you tell me how

  • Why "Prevent this page from creating additional dialogs" appears in the alert box? 2011-05-01

    In my Rails 3 application I do: render :js => "alert(\"Error!\\nEmpty message sent.\");" if ... Sometimes, below this error message (in the same alert box) I see: "Prevent this page from creating additional dialogs" and a checkbox. What does this

  • Iphone Alert Box Like Volume Buttons 2011-05-16

    i would like to create a message box similar to which appears when the volume buttons are pressed. Anyone knows how to do it? Thanks --------------Solutions------------- Check the MBProgressHUD project: https://github.com/matej/MBProgressHUD http://b

  • Is it possible to create a non-centered Javascript alert box? 2009-09-02

    For a standard Javascript alert() box, is it possible to have it appear in a position other than the center of the screen? Can you specify horizontal and/or vertical position? --------------Solutions------------- Afaik, it cannot be positioned where

  • How to make alert box return integer values on selected multple items? 2011-11-21

    I am building an alert box which has a char array of elements which is used as data for multiple selection checkbox. My question is how to make this alert box return the value as 1,2,3 depending on the selected item order? ie. if i select mercur and

  • Javascript close alert box 2009-01-20

    I want to be able to close an alert box automatically using javascript after a certain amount of time or on a specific event (i.e. onkeypress). From my research, it doesn't look like that's possible with the built-in alert() function. Is there a way

  • how do i write a javascript alert box to give a yes or no question and integrate with php calls? 2009-06-23

    i am trying to figure out how to create a javascript alert box asking the user if they would like to delete a record (that their viewing) and when the user presses yes a query is called through php to delete a database row. and if the user presses no

  • how do i write a javascript alert box to give a yes or no question and integrate with php calls? (repost) 2009-06-23

    i'm reposting this post i just posted a while ago. http://stackoverflow.com/questions/1035266/how-do-i-write-a-javascript-alert-box-to-give-a-yes-or-no-question-and-integrate/1035285#1035285 i wasnt getting anymore responses so i figured the post got

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.159 (s). 11 q(s)