- 弹窗选择
// 弹窗选择
func alertSheet(_ title: String?, _ message: String?, list: [String], result: @escaping ((_ index: Int) -> ())) {
let current = UIViewController.current()
let alert = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
var index = 0
for title in list {
let action = UIAlertAction(title: title, style: .default, handler: {
(action) in
let tag = action.info?["tag"] as! Int
result(tag)
})
action.info = ["tag": index]
alert.addAction(action)
index += 1
}
alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
current?.present(alert, animated: true, completion: nil)
}
使用方式:
2.弹窗输入
//弹窗输入 secret input 是密码输入类型,两种输入类型的结果会拼接在返回的contents里,按序取
func alertInput(_ title: String?, _ message: String?, inputPlaceholders: [String]?, secretInputPlaceholders: [String]?, userResponse: @escaping ((_ entry: Bool, _ inputContents: [String]) -> ())) {
let current = UIViewController.current()
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
if inputPlaceholders != nil {
for placeholder in inputPlaceholders! {
alert.addTextField {
(tf) in
tf.placeholder = placeholder
}
}
}
if secretInputPlaceholders != nil {
for placeholder in secretInputPlaceholders! {
alert.addTextField {
(tf) in
tf.placeholder = placeholder
tf.isSecureTextEntry = true
}
}
}
alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: {
(action) in
userResponse(false, [])
}))
alert.addAction(UIAlertAction(title: "确定", style: .default, handler: {
(action) in
var contents = [String]()
for tf in alert.textFields ?? [] {
contents.append(tf.text ?? "")
}
userResponse(true, contents)
}))
current?.present(alert, animated: true, completion: nil)
}
其他相关的简单弹窗交互
// 弹窗警告
func alert(_ title: String?, _ message: String?) {
DispatchQueue.main.async {
let current = UIViewController.current()
let alert = UIAlertController(tit