这是本人收集的一个AlertController延展类,感觉用起来比较简单,就和大家分享一下,以后用到Alert的时候只需要调用一个方法即可!(swift)
看代码:
extension UIAlertController {
class func showAlert(
presentController: UIViewController!,
title: String!,
message: String!,
cancelButtonTitle: String? = "取消",
okButtonTitle: String? = "确定") {
let alert = UIAlertController(title: title!, message: message!, preferredStyle: UIAlertControllerStyle.Alert)
if (cancelButtonTitle != nil) {
alert.addAction(UIAlertAction(title: cancelButtonTitle!, style: UIAlertActionStyle.Default, handler: nil))// do not handle cancel, just dismiss
}
if (okButtonTitle != nil) {
alert.addAction(UIAlertAction(title: okButtonTitle!, style: UIAlertActionStyle.Default, handler: nil))// do not handle cancel, just dismiss
}
presentController!.presentViewController(alert, animated: true, completion: nil)
}
class func showAlert(
presentController: UIViewController!,
title: String!,
message: String!,
cancelButtonTitle: String? = "取消",
okButtonTitle: String? = "确定",
okHandler: ((UIAlertAction!) -> Void)!) {
let alert = UIAlertController(title: title!, message: message!, preferredStyle: UIAlertControllerStyle.Alert)
if (cancelButtonTitle != nil) {
alert.addAction(UIAlertAction(title: cancelButtonTitle!, style: UIAlertActionStyle.Default, handler: nil))// do not handle cancel, just dismiss
}
if (okButtonTitle != nil) {
alert.addAction(UIAlertAction(title: okButtonTitle!, style: UIAlertActionStyle.Default, handler: okHandler))// do not handle cancel, just dismiss
}
presentController!.presentViewController(alert, animated: true, completion: nil)
}
}