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?
Go to Settings and tap Notifications. Select an app under Notification Style. Under Alerts, choose the alert style that you want. If you turn on Allow Notifications, choose when you want the notifications delivered — immediately or in the scheduled notification summary.
Alerts convey important information related to the state of your app or the device. An alert consists of a title, an optional message, one or more buttons, and optional text fields for gathering input.
Go to Settings > Mail > Notifications, then make sure that Allow Notifications is on. Tap Customize Notifications, then tap the email account you want to make changes to. Select the settings you want, like Alerts or Badges.
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:
EDIT: The code crashed on iPad, iOS 8+. If added the necessary lines as described here: on another stack overflow answer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With