封装系统弹框及元组作为函数参数的使用
2018-08-17 本文已影响26人
本帅不良
声明:作者将系统弹框进行了封装,但实际用处不大,它并没有很好的简化代码。
先上代码,以下是我对系统弹框的封装
import UIKit
class ToolManager: NSObject {
static let shareInstance : ToolManager = ToolManager()
/// - Parameters:
/// - viewController: 当前视图控制器
/// - title: 弹框标题
/// - message: 弹框内容
/// - style: 弹框样式
/// - action1: 操作1元组(操作名,操作回调)
/// - action2: 操作2元组(操作名,操作回调)
func showAlert(viewController:UIViewController,
title:String?,message:String?,
style:UIAlertControllerStyle,
action1: (_:String,_:(UIAlertAction) -> Void)?,
action2: (_:String,_:(UIAlertAction) -> Void)?) {
let ac = UIAlertController.init(title: title, message: message, preferredStyle: style)
if action1 != nil {
ac.addAction(UIAlertAction.init(title:action1?.0 , style: UIAlertActionStyle.default, handler: action1?.1))
}
if action2 != nil {
ac.addAction(UIAlertAction.init(title: action2?.0, style: UIAlertActionStyle.default, handler: action2?.1))
}
viewController.present(ac, animated: true, completion: nil)
}
}
封装中,我使用了元组将操作名与操作回调整合在一起了,按理说应该很完美!但实际使用起来却不尽如人意
如下图,一切的赋值都还好![](https://img.haomeiwen.com/i3255919/d5db3feee85b4fe9.png)
![](https://img.haomeiwen.com/i3255919/14d223abdbc3a652.png)
![](https://img.haomeiwen.com/i3255919/b5e85c41e6e417b9.png)
![](https://img.haomeiwen.com/i3255919/0df689c82eb8e721.png)
![](https://img.haomeiwen.com/i3255919/49c33f24af544e1e.png)
![](https://img.haomeiwen.com/i3255919/29b29e8963c75873.png)
这一系列的操作下来,封装的意义何在?还不如直接用系统的弹框。。