由于自己項目中需要彈窗的地方很多。癣防。所以找了資料自己寫了一個簡單的蜗巧。。
喜歡的自己拿去用吧蕾盯。希望能幫助一些同學(xué)幕屹。不多說直接上代碼。
funcpresentAlertVc(confirmBtn:String?, message:String, title:String, cancelBtn:String, handler:@escaping(UIAlertAction) ->Void, viewController:UIViewController) {
letalertVc =UIAlertController(title: title, message: message, preferredStyle: .alert)
letcancelAction =UIAlertAction(title: cancelBtn, style: .cancel, handler:nil)
alertVc.addAction(cancelAction)
ifconfirmBtn !=nil{
letokAction =UIAlertAction(title: confirmBtn, style: .default, handler: { (action)in
handler(action)
})
alertVc.addAction(okAction)
}
viewController.present(alertVc, animated:true, completion:nil)
}
這樣在你需要彈窗的時候就比較簡單了
直接調(diào)用:
只有一個按鈕的彈窗
presentAlertVc(confirmBtn: nil, message: "這是一個彈窗", title: "提示", cancelBtn: "好的", handler: { (actin) in
// your handler demo
}, viewController: self)
兩個按鈕的彈窗
presentAlertVc(confirmBtn: "OK", message: "這是一個彈窗", title: "提示", cancelBtn: "cancel", handler: { (actin) in
// your handler demo
}, viewController: self)
---來自濤胖子的工作筆記