Cómo agregar una acción a un button UIAlertView usando Swift iOS

Quiero agregar otro button que no sea el button "Aceptar", que debería descartar la alerta. Quiero que el otro button llame a cierta function.

var logInErrorAlert: UIAlertView = UIAlertView() logInErrorAlert.title = "Ooops" logInErrorAlert.message = "Unable to log in." logInErrorAlert.addButtonWithTitle("Ok") 

¿Cómo agrego otro button a esta alerta y luego le permito llamar a una function una vez que hace clic, así que digamos que queremos que llame el nuevo button:

  retry() 

La forma Swifty es usar el nuevo UIAlertController y los cierres:

  // Create the alert controller let alertController = UIAlertController(title: "Title", message: "Message", prefernetworkingStyle: .Alert) // Create the actions let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { UIAlertAction in NSLog("OK Pressed") } let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { UIAlertAction in NSLog("Cancel Pressed") } // Add the actions alertController.addAction(okAction) alertController.addAction(cancelAction) // Present the controller self.presentViewController(alertController, animated: true, completion: nil) 
 let alert = UIAlertController(title: "Alert", message: "My Alert for test", prefernetworkingStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction!) in print("you have pressed the ok button") })) alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: nil)) self.present(alert, animated: true, completion: nil) 

UIAlertViews usa un delegado para comunicarse con usted, el cliente.

Agregue un segundo button y cree un object para recibir los posts de delegado de la vista:

 class LogInErrorDelegate : UIAlertViewDelegate { init {} // not sure of the prototype of this, you should look it up func alertView(view :UIAlertView, clickedButtonAtIndex :Integer) -> Void { switch clickedButtonAtIndex { case 0: userClickedOK() // er something case 1: userClickedRetry() /* Don't use "retry" as a function name, it's a reserved word */ default: userClickedRetry() } } /* implement rest of the delegate */ } logInErrorAlert.addButtonWithTitle("Retry") var myErrorDelegate = LogInErrorDelegate() logInErrorAlert.delegate = myErrorDelegate 

Vea mi código:

  @IBAction func foundclicked(sender: AnyObject) { if (amountTF.text.isEmpty) { let alert = UIAlertView(title: "Oops! Empty Field", message: "Please enter the amount", delegate: nil, cancelButtonTitle: "OK") alert.show() } else { var alertController = UIAlertController(title: "Confirm Bid Amount", message: "Final Bid Amount : "+amountTF.text , prefernetworkingStyle: .Alert) var okAction = UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default) { UIAlertAction in JHProgressHUD.shanetworkingHUD.loaderColor = UIColor.networkingColor() JHProgressHUD.shanetworkingHUD.showInView(self.view, withHeader: "Amount registering" , andFooter: "Loading") } var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { UIAlertAction in alertController .removeFromParentViewController() } alertController.addAction(okAction) alertController.addAction(cancelAction) self.presentViewController(alertController, animated: true, completion: nil) } } 

Versión Swift 3.0 de la respuesta de Jake

// Crear el controller de alerta

 let alertController = UIAlertController(title: "Alert!", message: "There is no items for the current user", prefernetworkingStyle: .alert) // Create the actions let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { UIAlertAction in NSLog("OK Pressed") } let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { UIAlertAction in NSLog("Cancel Pressed") } // Add the actions alertController.addAction(okAction) alertController.addAction(cancelAction) // Present the controller self.present(alertController, animated: true, completion: nil) 

basado en swift:

 let alertCtr = UIAlertController(title:"Title", message:"Message", prefernetworkingStyle: .Alert) let Cancel = AlertAction(title:"remove", style: .Default, handler: {(UIAlertAction) -> Void in }) let Remove = UIAlertAction(title:"remove", style: .Destructive, handler:{(UIAlertAction)-> Void inself.colorLabel.hidden = true }) alertCtr.addAction(Cancel) alertCtr.addAction(Remove) self.presentViewController(alertCtr, animated:true, completion:nil)}