Swift - 警告提示框(UIAlertController)的用法
2016-06-25 13:46
501 查看
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { // 创建 let alertController = UIAlertController(title: "提示", message: "你确定要离开?", preferredStyle:.Alert) // 设置2个UIAlertAction let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil) let okAction = UIAlertAction(title: "好的", style: .Default) { (UIAlertAction) in print("点击了好的") } // 添加 alertController.addAction(cancelAction) alertController.addAction(okAction) // 弹出 self.presentViewController(alertController, animated: true, completion: nil) } }
// 除了弹出,还可以使用底部向上滑出的样式 // 注意:如果上拉菜单中有『取消』按钮的话,那么它永远都会出现在菜单的底部,不管添加的次序如何 // 创建 // preferredStyle 为 ActionSheet let alertController = UIAlertController(title: "保存或删除数据", message: "删除数据将不可恢复", preferredStyle:.ActionSheet) // 设置2个UIAlertAction let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil) let deleteAction = UIAlertAction(title: "删除", style: .Destructive, handler: nil) let saveAction = UIAlertAction(title: "保存", style: .Default, handler: nil) // 添加到UIAlertController alertController.addAction(cancelAction) alertController.addAction(saveAction) alertController.addAction(deleteAction) // 弹出 self.presentViewController(alertController, animated: true, completion: nil)
/* 添加任意数量的文本输入框(比如可以用来实现登录框) */ let alertController = UIAlertController(title: "系统登录", message: "请输入用户名和密码", preferredStyle: UIAlertControllerStyle.Alert) alertController.addTextFieldWithConfigurationHandler { (textField:UITextField) in textField.placeholder = "用户名" } alertController.addTextFieldWithConfigurationHandler { (textField:UITextField) in textField.placeholder = "密码" textField.secureTextEntry = true } let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil) let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Default) { (UIAlertAction) in let login = alertController.textFields![0] let pwd = alertController.textFields![1] print("用户名:\(login.text) 密码:\(pwd.text)") } alertController.addAction(cancelAction) alertController.addAction(okAction) // 弹出 self.presentViewController(alertController, animated: true, completion: nil)
相关文章推荐
- Swift入门基础语法
- Swift - 图像控件(UIImageView)的用法
- Swift - 分段选择控件(UISegmentedControl)的用法
- Swift 3.0(一)
- swift-5类型别名和布尔值
- Swift - 开关按钮(UISwitch)的用法
- Swift - 多行文本输入框(UITextView)的用法
- swift:闭包的解析
- Swift-传值
- Swift滑动手势
- Swift中设置tableview的分割线(separator)的样式、颜色、边距
- 自学php,用php服务端和swift客户端实现验证登录和注册功能 1
- 详解Swift中的函数及函数闭包使用
- Swift - 文本输入框(UITextField)的用法
- Swift 析构器deinit
- Swift学习---基础三(区间运算符、控制器转移语句)
- 复习Swift
- swift2.0 学习笔记Nineteen
- 跟着洲洲哥一块学习Swift-数组
- Swift - 按钮(UIButton)的用法