Nik*_*tin 63

在搜索了一个主题后,我没有找到明确的解释,即使在它的类引用 UIAlertController Reference中也是如此

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

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

所以这里:

  1. UIAlertView如指出的那样弃用: Swift中的UIAlertView
  2. UIAlertController 应该在iOS8 +中使用,所以首先创建一个我们需要实例化它,构造函数(init)获取3个参数:

2.1 title:String - >大型粗体文本,显示在alert的对话框顶部

2.2消息:字符串 - >较小的文本(几乎解释了它的自我)

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

  1. 现在要向用户显示它,我们可以使用 showViewControllerpresentViewController传递我们的警报作为参数

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

4.1 UIAlertController.addAction创建按钮

4.2 UIAlertController.addTextField创建文本字段

编辑注释:下面的代码示例,针对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);
}
Run Code Online (Sandbox Code Playgroud)

示例2:带有一个输入textField和两个按钮的对话框

@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);
}
Run Code Online (Sandbox Code Playgroud)

示例3:一个自定义输入textField和一个按钮

@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);
 }
Run Code Online (Sandbox Code Playgroud)

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

  • 我很高兴你分享了,你学到了什么!! 非常感谢您一步一步的解释..像志同道合;) (2认同)

Ome*_*han 11

UIAlertController的一个实例可以在屏幕上以模态方式呈现,就像使用presentViewController:animated:completion:方法的任何其他UIViewController一样.UIAlertController实例作为ActionSheet或AlertView的区别是创建它时传递的样式参数.

不再代表团了

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

对于行动表

@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)
}
Run Code Online (Sandbox Code Playgroud)

对于带文本字段的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)
}
Run Code Online (Sandbox Code Playgroud)