如何使用Swift iOS将操作添加到UIAlertViewbutton

我想添加另一个button,而不是“OK”button,应该closures警报。 我想要另一个button来调用某个function。

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

如何添加另一个button到这个警报,然后让它调用一个function,一旦点击,让我们说我们希望新的button来调用:

  retry() 

Swifty方法是使用新的UIAlertController和闭包:

  // Create the alert controller let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .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", preferredStyle: 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使用委托与您,客户进行通信。

您添加第二个button,并创build一个对象来接收来自视图的委托消息:

 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 

看我的代码:

  @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 , preferredStyle: .Alert) var okAction = UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default) { UIAlertAction in JHProgressHUD.sharedHUD.loaderColor = UIColor.redColor() JHProgressHUD.sharedHUD.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) } } 

Swift 3.0杰克的答案的版本

//创build警报控制器

 let alertController = UIAlertController(title: "Alert!", message: "There is no items for the current user", preferredStyle: .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) 

基于swift:

 let alertCtr = UIAlertController(title:"Title", message:"Message", preferredStyle: .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)} 

Swift 4更新

  // Create the alert controller let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .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)