Affichage d’alerte rapide (iOS8) avec le bouton OK et annuler, quel bouton a été tapé

J’ai une vue d’alerte dans Xcode écrite dans Swift et j’aimerais déterminer quel bouton l’utilisateur a sélectionné (c’est une boîte de dialog de confirmation) pour ne rien faire ou pour exécuter quelque chose. Actuellement j’ai:

@IBAction func pushedRefresh(sender: AnyObject) { var refreshAlert = UIAlertView() refreshAlert.title = "Refresh?" refreshAlert.message = "All data will be lost." refreshAlert.addButtonWithTitle("Cancel") refreshAlert.addButtonWithTitle("OK") refreshAlert.show() } 

J’utilise probablement mal les boutons, corrigez-moi s’il vous plait car c’est tout nouveau pour moi.

Je vous remercie!

Si vous utilisez iOS8, vous devez utiliser UIAlertController – UIAlertView est obsolète .

Voici un exemple d’utilisation:

 var refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertControllerStyle.Alert) refreshAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in print("Handle Ok logic here") })) refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in print("Handle Cancel Logic here") })) presentViewController(refreshAlert, animated: true, completion: nil) 

Comme vous pouvez le voir, les gestionnaires de blocs pour UIAlertAction gèrent les boutons. Un excellent tutoriel est ici (bien que ce tutoriel ne soit pas écrit avec swift): http://hayageek.com/uialertcontroller-example-ios/

Swift 3 mise à jour:

 let refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertControllerStyle.alert) refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in print("Handle Ok logic here") })) refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in print("Handle Cancel Logic here") })) present(refreshAlert, animated: true, completion: nil) 
 var refreshAlert = UIAlertController(title: "Log Out", message: "Are You Sure to Log Out ? ", preferredStyle: UIAlertControllerStyle.Alert) refreshAlert.addAction(UIAlertAction(title: "Confirm", style: .Default, handler: { (action: UIAlertAction!) in self.navigationController?.popToRootViewControllerAnimated(true) })) refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in refreshAlert .dismissViewControllerAnimated(true, completion: nil) })) presentViewController(refreshAlert, animated: true, completion: nil) 

Vous pouvez facilement faire cela en utilisant UIAlertController

 let alertController = UIAlertController( title: "Your title", message: "Your message", preferredStyle: .alert) let defaultAction = UIAlertAction( title: "Close Alert", style: .default, handler: nil) //you can add custom actions as well alertController.addAction(defaultAction) present(alertController, animated: true, completion: nil) 

.

Référence: iOS Show Alert

Mis à jour pour swift 3:

// définition de la fonction:

 @IBAction func showAlertDialog(_ sender: UIButton) { // Declare Alert let dialogMessage = UIAlertController(title: "Confirm", message: "Are you sure you want to Logout?", preferredStyle: .alert) // Create OK button with action handler let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in print("Ok button click...") self.logoutFun() }) // Create Cancel button with action handlder let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in print("Cancel button click...") } //Add OK and Cancel button to dialog message dialogMessage.addAction(ok) dialogMessage.addAction(cancel) // Present dialog message to user self.present(dialogMessage, animated: true, completion: nil) } 

// définition de la fonction logoutFun ():

 func logoutFun() { print("Logout Successfully...!") }