我们平时需要做的很多重复性工作例如服务器的返回状态对应不同的弹窗提示,封装弹窗提示是非常必要的,首先给出弹窗警告、弹窗确认、弹窗输入获取返回字符串、弹窗选择等:
// 弹窗警告
func alert(_ title: String?, _ message: String?) {
DispatchQueue.main.async {
let current = UIViewController.current()
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "确定", style: .default, handler: nil))
current?.present(alert, animated: true, completion: nil)
}
}
//弹窗确认
func confirm(_ title: String?, _ message: String?, choose: ( @escaping (_ confirm: Bool) -> ())) {
let current = UIViewController.current()
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: { (action) in
choose(false)
}))
alert.addAction(UIAlertAction(title: "确定", style: .default, handler: { (action) in
choose(true)
}))
current?.present(alert, animated: true, completion: nil)
}
//弹窗确认
func alert(_ title: String?, _ message: String?, click: ( @escaping () -> ())) {
let current = UIViewController.current()
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "确定", style: .default, handler: { (action) in
click()
}))
current?.present(alert, animated: true, completion: nil)
}
//弹窗输入 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 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)
}
使用封装好的alert()方法后,我们可以这样区分状态并弹窗:
guard status >= 0 else {
var promt = ""
switch status {
case -1://-1表示操作失败
promt = "操作失败"
break
case -2://-2表示手机号已注册
promt = "手机号已注册"
break
case -3://-3表示客户端未登陆
promt = "客户端未登陆"
break
case -4://-4表示验证超时
promt = "验证超时"
break
case -5://-5表示验证码不正确
promt = "验证码不正确"
break
default: break
}
alert(promt, nil)
return
}
但是用起来还是特别繁琐,所以我们继续封装状态的区分:
func switchStatusAndPromt(_ value: Any, _ status: [Any], _ promt: [String]) -> String {
var switchPromt = ""
let value = String(format: "%@", value as? NSObject ?? NSObject())
var index = 0
for i in status.indices {
let item = status[i]
let compare = String(format: "%@", item as? NSObject ?? NSObject())
if compare == value {
index = i
}
}
switchPromt = promt[index]
return switchPromt
}
这样我们就可以根据数据获得预设的状态值和提示的映射,使用起来是这样的:
let status = JSON(statusString).intValue
guard status >= 0 else {
alert(switchStatusAndPromt(status, [-1, -2, -3, -4, -5], ["操作失败", "手机号已注册", "客户端未登陆", "验证超时", "验证码不正确"]), nil)
return
}
效果: