在哪里可以find关于快速警报(UIAlertController)的清晰解释?

找不到一个明确的和翔实的解释。

在一个主题上search一段时间后,我没有find一个清晰的解释,甚至在它的类参考UIAlertController参考

没关系,但对我来说还不够清楚。

所以收集了一些和平之后,我决定做出我自己的解释(希望它有帮助)

所以在这里:

  1. UIAlertView已被废弃,如下所示: UIAlertView在Swift中
  2. UIAlertController应该在iOS8 +中使用,所以要创build一个我们需要实例化它,构造函数(init)得到3个参数:

2.1标题:string – >在警告对话框的顶部显示的粗体文本

2.2消息:string – >较小的文本(几乎可以解释它是自己的)

2.3 prefferedStyle:UIAlertControllerStyle – >定义对话框样式,在大多数情况下: UIAlertControllerStyle.Alert

  1. 现在实际显示给用户,我们可以使用showViewControllerpresentViewController ,并将我们的警报作为parameter passing

  2. 为了添加一些与用户的交互,我们可以使用:

4.1 UIAlertController.addAction创buildbutton

4.2 UIAlertController.addTextField创build文本字段

编辑注:下面的代码示例,更新为swift 3语法

示例1 :简单对话框

 @IBAction func alert1(sender: UIButton) { //simple alert dialog let alert=UIAlertController(title: "Alert 1", message: "One has won", preferredStyle: UIAlertControllerStyle.alert); //show it show(alert, sender: self); } 

示例2 :带有一个input文本字段和两个button的对话框

 @IBAction func alert2(sender: UIButton) { //Dialog with one input textField & two buttons let alert=UIAlertController(title: "Alert 2", message: "Two will win too", preferredStyle: UIAlertControllerStyle.alert); //default input textField (no configuration...) alert.addTextField(configurationHandler: nil); //no event handler (just close dialog box) alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.cancel, handler: nil)); //event handler with closure alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction) in let fields = alert.textFields!; print("Yes we can: "+fields[0].text!); })); present(alert, animated: true, completion: nil); } 

示例3 :一个自定义的input文本字段和一个button

 @IBAction func alert3(sender: UIButton) { // one input & one button let alert=UIAlertController(title: "Alert 3", message: "Three will set me free", preferredStyle: UIAlertControllerStyle.alert); //configured input textField var field:UITextField?;// operator ? because it's been initialized later alert.addTextField(configurationHandler:{(input:UITextField)in input.placeholder="I am displayed, when there is no value ;-)"; input.clearButtonMode=UITextFieldViewMode.whileEditing; field=input;//assign to outside variable(for later reference) }); //alert3 yesHandler -> defined in the same scope with alert, and passed as event handler later func yesHandler(actionTarget: UIAlertAction){ print("YES -> !!"); //print text from 'field' which refer to relevant input now print(field!.text!);//operator ! because it's Optional here } //event handler with predefined function alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: yesHandler)); present(alert, animated: true, completion: nil); } 

希望它有帮助,祝你好运;-)

UIAlertController的一个实例可以在屏幕上以与使用presentViewController:animated:completion:方法的其他UIViewController一样的模态显示。 什么使得UIAlertController实例作为一个ActionSheet或者AlertView来区分是你在创build时所传递的样式参数。

不再有代表团

如果您使用了UIActionSheet或UIAlertView,您知道从中获取callback的方法是为类(在大多数情况下为ViewController)实现UIActionSheetDelegate或UIAlertViewDelegate协议。 有一些开源项目用基于块的callback取代了这种委托模式,但官方的API从未更新过。 UIAlertController不使用委派。 相反,它有一个UIAlertAction项的集合,它使用闭包(如果使用Objective-C,则使用块)来处理用户input。

对于操作表

 @IBAction func showActionSheet(sender: AnyObject) { //Create the AlertController let actionSheetController: UIAlertController = UIAlertController(title: "Action Sheet", message: "Swiftly Now! Choose an option!", preferredStyle: .ActionSheet) //Create and add the Cancel action let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in //Just dismiss the action sheet } actionSheetController.addAction(cancelAction) //Create and add first option action let takePictureAction: UIAlertAction = UIAlertAction(title: "Take Picture", style: .Default) { action -> Void in //Code for launching the camera goes here } actionSheetController.addAction(takePictureAction) //Create and add a second option action let choosePictureAction: UIAlertAction = UIAlertAction(title: "Choose From Camera Roll", style: .Default) { action -> Void in //Code for picking from camera roll goes here } actionSheetController.addAction(choosePictureAction) //Present the AlertController self.presentViewController(actionSheetController, animated: true, completion: nil) } 

对于带有文本字段的AlertView

 @IBAction func showAlert(sender: AnyObject) { //Create the AlertController let actionSheetController: UIAlertController = UIAlertController(title: "Alert", message: "Swiftly Now! Choose an option!", preferredStyle: .Alert) //Create and add the Cancel action let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in //Do some stuff } actionSheetController.addAction(cancelAction) //Create and an option action let nextAction: UIAlertAction = UIAlertAction(title: "Next", style: .Default) { action -> Void in //Do some other stuff } actionSheetController.addAction(nextAction) //Add a text field actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in //TextField configuration textField.textColor = UIColor.blueColor() } //Present the AlertController self.presentViewController(actionSheetController, animated: true, completion: nil) }