Swift---UIAlertController封装

来源:互联网 发布:java 上传下载apk文件 编辑:程序博客网 时间:2024/05/16 05:02
import Foundationimport UIKitclass YJAlertControllerTool {    /**     alterController 两个按钮 处理otherBtn事件     - parameter currentVC: 当前控制器     - parameter meg:       提示消息     - parameter cancelBtn: 取消按钮     - parameter otherBtn:  其他按钮     - parameter handler:   其他按钮处理事件     */    static func showAlert(currentVC:UIViewController, meg:String, cancelBtn:String, otherBtn:String?, handler:((UIAlertAction) -> Void)?){//        guard let vc = self.getCurrentVC() else{ return }        dispatch_async(dispatch_get_main_queue(), { () -> Void in            let alertController = UIAlertController(title:GFB_ALTER_MSG,                message:meg ,                preferredStyle: .Alert)            let cancelAction = UIAlertAction(title:cancelBtn, style: .Cancel, handler:nil)            alertController.addAction(cancelAction)            if otherBtn != nil{                let settingsAction = UIAlertAction(title: otherBtn, style: .Default, handler: { (action) -> Void in                    handler?(action)                })                alertController.addAction(settingsAction)            }            currentVC.presentViewController(alertController, animated: true, completion: nil)        })    }    /**     alterController 一个按钮 不处理事件,简单实用     - parameter currentVC: 当前控制器     - parameter meg:       提示消息     */    static func showAlert(currentVC:UIViewController, cancelBtn:String, meg:String){        showAlert(currentVC, meg: meg, cancelBtn: cancelBtn, otherBtn: nil, handler: nil)    }    /**     两个按钮 都处理事件     - parameter currentVC:     当前控制器     - parameter meg:           提示消息     - parameter cancelBtn:     取消按钮     - parameter otherBtn:      其他按钮     - parameter cencelHandler: 取消按钮事件回调 (不处理可不写,考虑到有些场合需要使用)     - parameter handler:       其他按钮事件回调     */    static func showAlert(currentVC:UIViewController, meg:String, cancelBtn:String, otherBtn:String?,cencelHandler:((UIAlertAction) -> Void)?, handler:((UIAlertAction) -> Void)?){        dispatch_async(dispatch_get_main_queue(), { () -> Void in            let alertController = UIAlertController(title:GFB_ALTER_MSG,                message:meg ,                preferredStyle: .Alert)            let cancelAction = UIAlertAction(title:cancelBtn, style: .Cancel, handler:{ (action) -> Void in                cencelHandler?(action)            })            alertController.addAction(cancelAction)            if otherBtn != nil{                let settingsAction = UIAlertAction(title: otherBtn, style: .Default, handler: { (action) -> Void in                    handler?(action)                })                alertController.addAction(settingsAction)            }            currentVC.presentViewController(alertController, animated: true, completion: nil)        })    }}
0 0
原创粉丝点击