哪里可以找到关于 swift alert (UIAlertController) 的清晰解释?

时间:2023-04-24
本文介绍了哪里可以找到关于 swift alert (UIAlertController) 的清晰解释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

对此找不到清晰而翔实的解释.

Couldn't find a clear and informative explanation for this.

推荐答案

在一个主题上搜索了一段时间后我没有找到一个清晰的解释,即使在它的类参考中UIAlertController 参考

After searching a while on a subject I didn't find a clear explanation , even in it's class reference UIAlertController Reference

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

It is ok, but not clear enough for me.

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

So after collecting some peaces I decided to make my own explanation (Hope it helps)

就这样吧:

  1. UIAlertView 如所指出的那样被弃用:Swift 中的 UIAlertView
  2. UIAlertController 应该在 iOS8+ 中使用所以要先创建一个,我们需要实例化它,Constructor(init) 获取 3 个参数:
  1. UIAlertView is deprecated as pointed out : UIAlertView in Swift
  2. UIAlertController should be used in iOS8+ so to create one first we need to instantiate it, the Constructor(init) gets 3 parameters:

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

2.1 title:String -> big-bold text to display on the top of alert's dialog box

2.2 message:String -> 较小的文本(几乎可以解释它的自身)

2.2 message:String -> smaller text (pretty much explains it's self)

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

2.3 prefferedStyle:UIAlertControllerStyle -> define the dialog box style, in most cases: UIAlertControllerStyle.Alert

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

  1. Now to actually show it to the user, we can use showViewController or presentViewController and pass our alert as parameter

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

To add some interaction with a user we can use:

4.1UIAlertController.addAction 创建按钮

4.2UIAlertController.addTextField 创建文本字段

编辑说明:以下代码示例,已针对 swift 3 语法进行了更新

Edit note: code examples below, updated for swift 3 syntax

示例 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:带有一个输入 textField 的对话框 &两个按钮

Example 2: Dialog with one input textField & two buttons

@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:一个自定义输入 textField &一键

Example 3: One customized input textField & one 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);
 }

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

这篇关于哪里可以找到关于 swift alert (UIAlertController) 的清晰解释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何实现材料指南中描述的全屏对话框? 下一篇:在活动之上创建一个透明对话框

相关文章