自iOS8起,苹果就建议告警框使用UIAlertController来代替UIAlertView和UIActionSheel。下面总结了一些常见的用法:
注意:
书写代码的时候,如果在viewDidLoad方法中,需要通过GCD在开启一个线程,使用dispatch_async,而写在viewDidAppear方法中,就不需要开线程了。可以参考我之前写的UIAlertController_whose view is not in the window hierarchy!问题这篇文章
1 简单的应用 (同时按钮响应Handler使用的闭包函数)
效果图
代码
import UIKit
class AlertViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
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.presentViewController(alertController, animated: true, completion: nil)
}
}
2 除了弹出,还可以使用从底部向上滑出的样式
注意:
- 如果上拉菜单中有“取消”按钮的话,那么它永远都会出现在菜单的底部,不管添加的次序是如何
- 按钮使用“Destructive”样式,文字颜色变红,用来来警示用户
效果图
class AlertViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
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.presentViewController(alertController, animated: true, completion: nil)
}
}
3 添加任意数量文本输入框(比如可以用来实现个登陆框)
效果图
代码
import UIKit
class AlertViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let alertController = UIAlertController(title: "系统登陆", message: "请输入用户名和密码", preferredStyle: .Alert)
alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) in
textField.placeholder = "用户名"
}
alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) in
textField.placeholder = "密码"
textField.secureTextEntry = true
}
let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
let okAction = UIAlertAction(title: "好的", style: .Default) { (action) in
//也可以用下标的形式获取textField let login = alertController.textFields![0]
let login = (alertController.textFields?.first!)! as UITextField
let password = (alertController.textFields?.last)! as UITextField
print("用户名:\(login.text) 密码:\(password.text)")
}
alertController.addAction(cancelAction)
alertController.addAction(okAction)
// self.presentViewController(alertController, animated: animated, completion: nil)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
//在另外一个线程中处理这些操作,然后通知主线程更新界面。
dispatch_async(dispatch_get_main_queue(), {
self.presentViewController(alertController, animated: true, completion: nil)
})
}
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let alertController = UIAlertController(title: "系统登陆", message: "请输入用户名和密码", preferredStyle: .Alert)
alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) in
textField.placeholder = "用户名"
}
alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) in
textField.placeholder = "密码"
textField.secureTextEntry = true
}
let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
let okAction = UIAlertAction(title: "好的", style: .Default) { (action) in
//也可以用下标的形式获取textField let login = alertController.textFields![0]
let login = (alertController.textFields?.first!)! as UITextField
let password = (alertController.textFields?.last)! as UITextField
print("用户名:\(login.text) 密码:\(password.text)")
}
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.presentViewController(alertController, animated: animated, completion: nil)
}
}
4 使用代码移除提示框
self.presentedViewController?.dismissViewControllerAnimated(false, completion: nil)