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 b
Everyone's saying UIAlertView. But to confirm deletion, UIActionSheet is likely the better choice. See When to use a UIAlertView vs. UIActionSheet
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
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
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.