swift4.0

自定义UIAlertController

2018-05-07  本文已影响42人  279cb620c509

自定义UIAlertController,指定时间,消失继续执行block内容

1:显示alert,点击过到指定时间,dissMiss Alert

2:显示alert,点击过到指定时间,dissMiss Alert 后,继续执行block内容

classUtils:NSObject{

•staticvaralert:UIAlertController!

•// 消息提示框

•publicstaticfuncnotifyUser(title:String,message:String,timeToDissapear:Int)->Void

•{

•alert=UIAlertController(title:title,

•message:message,

•preferredStyle:UIAlertControllerStyle.alert)

•letcancelAction=UIAlertAction(title:"确定",

•style:.cancel,handler:{

•actionin

•return

•})

•alert.addAction(cancelAction)

•UIApplication.shared.keyWindow?.rootViewController!

•.present(alert,animated:true,completion:nil)

•//定时关闭

•_=Timer.scheduledTimer(timeInterval:Double(timeToDissapear),target:self,selector:#selector(Utils.dismissAlert),userInfo:nil,repeats:false)

•}

•@objcstaticfuncdismissAlert()

•{

•// Dismiss the alert from here

•alert.dismiss(animated:true,completion:nil)

•}

•typealiasZZWandBlock=(()->Void)

•varandBlock:ZZWandBlock?

•// 消息提示框

•funcnotifyUserB(title:String,message:String,timeToDissapear:Int,_block:@escaping(()->Void))->Void

•{

•andBlock=block

•Utils.alert=UIAlertController(title:title,

•message:message,

•preferredStyle:UIAlertControllerStyle.alert)

•letcancelAction=UIAlertAction(title:NSLocalizedString("BUTTON_OK",comment:"确定"),

•style:.cancel,handler:{

•actionin

•block()

•return

•})

•Utils.alert.addAction(cancelAction)

•UIApplication.shared.keyWindow?.rootViewController!

•.present(Utils.alert,animated:true,completion:nil)

•//        self.present(alert, animated: true) {

•//

•//        }

•//定时关闭

•_=Timer.scheduledTimer(timeInterval:Double(timeToDissapear),target:self,selector:#selector(Utils.dismissAlertB),userInfo:nil,repeats:false)

•}

•@objcfuncdismissAlertB()

•{

•// Dismiss the alert from here

•ifUtils.alert.isBeingDismissed{

•}else{

•Utils.alert.dismiss(animated:true){

•self.andBlock!()

•}

•}

•}

}

用法:

Utils.notifyUser(title:"tips",message:"请连接",timeToDissapear:3)

Utils().notifyUserB(title:"信息",message:"状态",timeToDissapear:3,{

•print("jbcdksbck")

•})

上一篇下一篇

猜你喜欢

热点阅读