在swift中alertView的初始化只允许创建拥有一个取消按钮的对话框视图
//定义一个UIAlertControll
var
alert =
UIAlertController
(title:
"
添加内容
"
, message:
"
请输入。。。
"
, preferredStyle:
UIAlertControllerStyle
.
Alert
)
//创建下面的保存按钮以及按钮的样式
let
saveAction =
UIAlertAction
(title:
"
保存
"
, style:
UIAlertActionStyle
.
Default
) { (action:
UIAlertAction
!) ->
Void
in
//创建一个textFiled对象,用于输入内容
let
textfiled = alert.
textFields
![
0
]
as
UITextField
}
//创建一个取消按钮
let
cancelAction =
UIAlertAction
(title:
"
取消
"
, style:
UIAlertActionStyle
.
Default
) { (action:
UIAlertAction
!) ->
Void
in
}
//讲这两个添加上去
alert.
addAction
(saveAction)
alert. addAction (cancelAction)
alert. addAction (cancelAction)
//添加文本
alert.
addTextFieldWithConfigurationHandler
{ (textFiled:
UITextField
!) ->
Void
in
}
//让alert弹出,显示出view控制器
self
.
presentViewController
(alert, animated:
true
, completion:
nil
)
相关注意的地方:
1: UIAlertController(title: <#String?#>, message: <#String?#>, preferredStyle: <#UIAlertControllerStyle#>)
preferredStyle及为alert显示样式,默认为default
2:保存取消按钮都需要去创建响应的AlertAction,并且要对其进行addAction()添加
3:文本框的创建需要创建textFileds[]数组(具体参看API)并对其 强制类型转换
4:对文本框进行添加
alert.
addTextFieldWithConfigurationHandler
5:最后让alert弹出如同show(),即
self
.
presentViewController
(alert, animated:
true
, completion:
nil
)