UIAlertView 可以使用,但是已被苹果弃用,创建UIAlertView有好几种不同的方式:
1、 let alterview = UIAlertView.init(title: "系统提示", message: "Hello World", delegate: self, cancelButtonTitle: "确定")
self.view.addSubview(alterview)
alterview.show()
2、 let alterview = UIAlertView.init(title: "系统", message: "Hello", delegate: self, cancelButtonTitle: "确定", otherButtonTitles: "取消","其他") //其他可以去到也可以后面再添加字符串
self.view.addSubview(alterview)
alterview.show()
3、 let alterview = UIAlertView()
alterview.title = "系统消息"
alterview.message = "Hello"
alterview.addButton(withTitle: "取消")
alterview.addButton(withTitle: "确认")
alterview.cancelButtonIndex = 0
alterview.delegate = self
self.view.addSubview(alterview)
alterview.show()
相关属性
样式:alterview.alertViewStyle = UIAlertViewStyle.loginAndPasswordInput 带有两个输入框
alterview.alertViewStyle = UIAlertViewStyle.plainTextInput 带有一个输入框
alterview.alertViewStyle = UIAlertViewStyle.secureTextInput 带有一个输入框,输入是隐藏的密码类型
alterview.alertViewStyle = UIAlertViewStyle.default 默认样式
方法:
func willPresent(_ alertView: UIAlertView) {
//警告框显示前别调用
}
func didPresent(_ alertView: UIAlertView) {
//警告框显示后被调用
}
func alertViewCancel(_ alertView: UIAlertView) {
//警告框显示中强制关闭时被调用,例如警告框显示时应用程序突然关闭等场合 触摸取消按钮时不会调用此方法
}
func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
//触摸警告框中任一按钮时被调用,此方法比alterview:willDismissWithButtonIndex 方法先被调用
if buttonIndex == alertView.cancelButtonIndex {
print("点击了取消")
}else{
print("点击了确认")
// 如果里面包含输入框,获取输入框中的信息
let name = alertView.textField(at: 0)
}
}
func alertView(_ alertView: UIAlertView, willDismissWithButtonIndex buttonIndex: Int) {
//警告框关闭前调用
}
func alertView(_ alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) {
//警告框关闭后被调用,警告框显示中应用程序进入睡眠状态时也会被调用
}
UIAlertController
//制作demo的时候再好放在其他控件里边调用,如果是直接写在viewDidLoad()有时候失效
let alterController = UIAlertController.init(title: "系统消息", message: "hello", preferredStyle: UIAlertControllerStyle.alert)
let cancleAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.cancel, handler: nil)
let okAction = UIAlertAction.init(title: "确认", style: UIAlertActionStyle.default, handler: nil)
alterController.addAction(cancleAction)
alterController.addAction(okAction)
self.present(alterController, animated: true, completion: nil)
带有输入框的
let alterController = UIAlertController.init(title: "系统消息", message: "hello", preferredStyle: UIAlertControllerStyle.alert)
let cancleAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.cancel, handler: nil)
alterController.addTextField { (textField:UITextField!) in
textField.placeholder = "登录"
}
alterController.addTextField {
(textField: UITextField!) -> Void in
textField.placeholder = "密码"
textField.isSecureTextEntry = true
}
let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.default) {
(action: UIAlertAction!) -> Void in
var login = (alterController.textFields?.first)! as UITextField
var password = (alterController.textFields?.last)! as UITextField
//处理事件在这里
}
alterController.addAction(cancleAction)
alterController.addAction(okAction)
self.present(alterController, animated: true, completion: nil)