swift alert弹框
1、创建BaseViewController
2、在BaseViewController中创建方法
extension BaseViewController{
func showAlert(title:String,message:String,ok:String,okBlock: @escaping () -> Void){
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let action1 = UIAlertAction(title:ok, style: .default) { (action) in
okBlock();
}
alertController.addAction(action1)
present(alertController, animated: true, completion: nil)
}
func showAlert(title:String,message:String,ok:String,cancel:String,okBlock: @escaping () -> Void,cancelBlock: @escaping () -> Void){
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let action1 = UIAlertAction(title:ok, style: .default) { (action) in
okBlock();
}
let action2 = UIAlertAction(title:cancel, style: .destructive) { (action) in
cancelBlock()
}
alertController.addAction(action1)
alertController.addAction(action2)
present(alertController, animated: true, completion: nil)
}
}
3、调用
控制器HomeController:BaseViewController{}
showAlert(title: "提示", message: "弹框", ok: "确定", cancel: "取消") {
} cancelBlock: {
}