在哪里可以找到关于快速警报(UIAlertController)的清晰解释? [英] Where to find a clear explanation about swift alert (UIAlertController)?
问题描述
在一个主题上搜索一段时间后,我没有找到清楚和翔实的解释。 t
找到一个明确的解释,即使在它的类参考
UIAlertController参考
没关系,但对我来说不够清楚。
所以在收集一些peaces之后,我决定做出自己的解释
(希望它有帮助)
所以这里:
2.1标题:字符串 - >显示在警报对话框顶部的大胆文本
2.2消息:字符串 - >较小的文本(几乎解释为自己)
2.3 prefferedStyle:UIAlertControllerStyle
- >定义对话框样式,在大多数情况下: UIAlertControllerStyle.Alert
-
向用户显示,我们可以使用
showViewController
或presentViewController
并将我们的警报作为参数 -
传递给我们可以使用的用户的一些交互:
4.1
UIAlertController.addAction
创建按钮
4.2
UIAlertController.addTextField
to cre at文本字段
编辑备注:下面的代码示例,更新为swift 3语法
示例1 :简单对话框
@IBAction func alert1(发件人:UIButton){
//简单警报对话框
let alert = UIAlertController(标题:Alert 1,消息:One was won,preferredStyle:UIAlertControllerStyle.alert);
//显示
show(alert,sender:self);
}
示例2 :具有一个输入textField和amp ;两个按钮
@IBAction func alert2(发件人:UIButton){
//与一个输入textField&两个按钮
let alert = UIAlertController(title:Alert 2,message:Two will win too,preferredStyle:UIAlertControllerStyle.alert);
//默认输入textField(无配置...)
alert.addTextField(configurationHandler:nil);
//没有事件处理程序(只是关闭对话框)
alert.addAction(UIAlertAction(title:No,style:UIAlertActionStyle.cancel,handler:nil));
//关闭事件处理函数
alert.addAction(UIAlertAction(title:Yes),style:UIAlertActionStyle.default,handler:{(action:UIAlertAction)in
let fields = alert。 textFields !;
print(是的,我们可以:+ fields [0] .text!);
}));
present(alert,animated:true,completion:nil);
}
示例3 :一个自定义输入textField&一个按钮
@IBAction func alert3(发件人:UIButton){
//一个输入&一个按钮
let alert = UIAlertController(标题:Alert 3,消息:Three will set me free,preferredStyle:UIAlertControllerStyle.alert);
//配置输入textField
var field:UITextField?; //运算符?因为它已被稍后初始化
alert.addTextField(configurationHandler:{(input:UITextField)in
input.placeholder =我没有显示时,没有值;-);
input.clearButtonMode = UITextFieldViewMode.whileEditing;
field = input; //赋值给外部变量(以供参考)
});
// alert3 yesHandler - >在同一范围内与alert进行定义,然后作为事件处理程序传递
func yesHandler(actionTarget:UIAlertAction){
print(YES - > !!);
//打印来自'field'的文本,现在引用相关的输入
print(field!.text!); // operator!因为它是可选这里
}
//具有预定义函数的事件处理程序
alert.addAction(UIAlertAction(title:Yes,style:UIAlertActionStyle.default,handler:yesHandler));
present(alert,animated:true,completion:nil);
}
希望有帮助,祝你好运; - )
Couldn't find a clear and informative explanation for this.
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)
So here it goes:
UIAlertView
is deprecated as pointed out : UIAlertView in SwiftUIAlertController
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 -> big-bold text to display on the top of alert's dialog box
2.2 message:String -> smaller text (pretty much explains it's self)
2.3 prefferedStyle:UIAlertControllerStyle
-> define the dialog box style, in most cases: UIAlertControllerStyle.Alert
Now to actually show it to the user, we can use
showViewController
orpresentViewController
and pass our alert as parameterTo add some interaction with a user we can use:
4.1
UIAlertController.addAction
to create buttons
4.2
UIAlertController.addTextField
to create text fields
Edit note: code examples below, updated for swift 3 syntax
Example 1: Simple Dialog
@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);
}
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);
}
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);
}
Hope It helps, and good luck ;-)
这篇关于在哪里可以找到关于快速警报(UIAlertController)的清晰解释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!