声明:作者将系统弹框进行了封装,但实际用处不大,它并没有很好的简化代码。
先上代码,以下是我对系统弹框的封装
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)
}
}
封装中,我使用了元组将操作名与操作回调整合在一起了,按理说应该很完美!但实际使用起来却不尽如人意
如下图,一切的赋值都还好





这一系列的操作下来,封装的意义何在?还不如直接用系统的弹框。。
网友评论