UIAlertController提示框
//创建AlertView,
let alertView = UIAlertController(title: "提示", message: "这是一个警告⚠️", preferredStyle: .alert)
//创建两个提示按钮 取消 至少有一个按钮,不然UIAlertController是无法返回的
//如果需要点击后出发某个事件,可以在handler:这个属性后面的闭包中处理
let alertActionCancel = UIAlertAction(title: "取消", style: .cancel, handler: nil)
//确定
let alertActionOK = UIAlertAction(title: "确定", style: .default) { (UIAlertAction) in
//确定的操作
print("你点击了确定按钮")
}
//添加一个textField然后在闭包中设置一些属性
alertView.addTextField { (UITextField) in
UITextField.placeholder = "username"
UITextField.clearButtonMode = .whileEditing
//设置监听,获取输入框输入的值
NotificationCenter.default.addObserver(self, selector: #selector(self.textChang(notification:)), name: NSNotification.Name.UITextFieldTextDidChange, object: UITextField)
}
alertView.addTextField { (UITextField) in
UITextField.placeholder = "password"
UITextField.clearButtonMode = .whileEditing
UITextField.isSecureTextEntry = true
}
//将按钮添加到弹窗上
alertView.addAction(alertActionCancel);
alertView.addAction(alertActionOK)
//弹出提示框
self.present(alertView, animated: true, completion: nil)
当文本框值改变时调用方法
func textChang(notification: NSNotification) {
//获取输入框对象
let textField = notification.object as! UITextField
myLabel?.text = textField.text
}
释放监听
deinit {
NotificationCenter.default.removeObserver(self)
}
UIAlertController的两种风格的实现差不多,知道这个就行了