Swift中UIAlertController的使用
2015-03-27 16:53
134 查看
在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.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)
//定义一个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.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)
相关文章推荐
- Swift 3.0 使用Core Data
- GCD学习 - swift GCD使用指南
- swift--swift动画库spring使用和代码拆解
- RxSwift基本使用(一)
- Swift 类构造器的使用
- Swift - 从相册中选择照片并上传(使用UIImagePickerController)
- 学习笔记:WKWebView的简单使用(Swift)
- Swift版使用ThPullRefresh实现下拉上拉刷新数据
- Swift-CGRect,CGSize,CGPoint 使用
- swift学习之路之UITabBrarController的简单使用
- swift中可选值?和!使用的方法示例
- 【iOS10】使用XCode8、Swift3. 操作SQLite3数据库的步骤(包括配置环境和SQLite语句)
- Swift值字典使用
- ios swift开发中,使用键盘,tableview根据键盘调整高度
- OC项目中使用Swift
- swift 中使用扩展实现代理,让程序猿省心,让代码简洁
- 使用curl操作openstack swift
- Block块/Swift闭包介绍与使用
- 使用Swauth认证多节点安装Swift
- Swift 链表 的制作 使用