swift - UIAlertController 的用法
2016-07-22 13:33
441 查看
ios 8 以后苹果官方建议使用UIAlertController这个类,所以专门去网上找资料,了解了下用法,
1、创建一个alertController
2、创建一个actionSheet
(注:如果上拉菜单中有“取消”按钮的话,那么它永远都会出现在菜单的底部,不管添加的次序是如何)
3、按钮使用警告模式,文字颜色变化,用来警示用户
4、添加任意数量的文本输入框
如图
![](https://images2015.cnblogs.com/blog/750937/201607/750937-20160722133205576-1130515275.png)
5、使用代码移除提示框
1、创建一个alertController
let alertController = UIAlertController(title: "系统提示", message: "您确定要离开吗?", preferredStyle: .alert) let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil) let okAction = UIAlertAction(title: "好的", style: .default, handler: { action in print("点击了确定") }) alertController.addAction(cancelAction) alertController.addAction(okAction) self.present(alertController, animated: true, completion: { //这里可以做一些其他操作 })
2、创建一个actionSheet
(注:如果上拉菜单中有“取消”按钮的话,那么它永远都会出现在菜单的底部,不管添加的次序是如何)
let alertController = UIAlertController(title: "保存或删除数据", message: "删除数据将不可恢复", preferredStyle: .actionSheet) let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil) let deleteAction = UIAlertAction(title: "删除", style: .destructive, handler: nil) let archiveAction = UIAlertAction(title: "保存", style: .default, handler: nil) alertController.addAction(cancelAction) alertController.addAction(deleteAction) alertController.addAction(archiveAction) self.present(alertController, animated: true, completion: nil)
3、按钮使用警告模式,文字颜色变化,用来警示用户
var okAction = UIAlertAction(title: "好的", style: .destructive, handler: nil)
4、添加任意数量的文本输入框
let myAlertController = UIAlertController(title:"系统登录",message:"请输入用户名和密码",preferredStyle:.alert) myAlertController.addTextField { (textField:UITextField) in textField.placeholder = "用户名" } myAlertController.addTextField { (textField:UITextField) in textField.placeholder = "密码" textField.isSecureTextEntry = true } myAlertController.addTextField { (textField:UITextField) in textField.placeholder = "重复密码" textField.isSecureTextEntry = true } let cancelAction = UIAlertAction(title:"取消",style:.cancel,handler:nil) let okAction = UIAlertAction(title:"确定",style:.default, handler:{ action in let login = myAlertController.textFields?.first let passWord = myAlertController.textFields?.last print("用户名是:\(String(describing: login)) 密码是:\(String(describing: passWord))") }) myAlertController.addAction(cancelAction) myAlertController.addAction(okAction) self.present(myAlertController, animated: true) { self.view.backgroundColor = UIColor.blue }
如图
![](https://images2015.cnblogs.com/blog/750937/201607/750937-20160722133205576-1130515275.png)
5、使用代码移除提示框
self.dismiss(animated: true) { //其他操作 }
相关文章推荐
- swift笔记
- Swift UILable 设置内边距实例代码
- Swift 延迟运行代码
- Swift 菊花、UIPageControl和UIProgressView
- Swift 共享文件操作小结(iOS 8 +)
- Swift UITextField各种属性的设置
- swift - UIAlertView 的用法
- Swift 动画片段
- iOS 疑难杂症 — — Swift debugger 无法在控制台 po 变量值的问题
- Swift 发送邮件和发短信
- Swift GCD
- Swift 2.1 为 UIView 添加点击事件和点击效果
- swift - UISlider 的用法
- iOS--多线程具体总结(OC&Swift)
- Swift 面向对象解析(二)
- 最详尽的 Swift 代码规范指南
- (Swift)Missing argument for parameter #1 in call
- Swift中闭包用法详解
- swift学习笔记 - Unicode转码
- swift学习笔记 - Unicode转码