美文网首页swift4.0
swift自定Alert,指定时间,消失继续执行block内容

swift自定Alert,指定时间,消失继续执行block内容

作者: 279cb620c509 | 来源:发表于2018-11-22 18:13 被阅读42次

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

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

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

class Utils :NSObject{

    static var alert: UIAlertController!

      // 消息提示框

    public static func notifyUser(title: String, message: String, timeToDissapear: Int) -> Void

    {

        alert = UIAlertController(title: title,

                                  message: message,

                                  preferredStyle: UIAlertControllerStyle.alert)

        let cancelAction = UIAlertAction(title: "确定",

                                        style: .cancel, handler: {

                                            action in

                                            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)

    }

    @objc static func dismissAlert()

    {

        // Dismiss the alert from here

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

    }

    typealias ZZWandBlock = (() -> Void)

    var andBlock : ZZWandBlock?

    // 消息提示框

    func notifyUserB(title: String, message: String, timeToDissapear: Int,_ block: @escaping (() -> Void)) -> Void

    {

        andBlock = block

        Utils.alert = UIAlertController(title: title,

                                        message: message,

                                        preferredStyle: UIAlertControllerStyle.alert)

        let cancelAction = UIAlertAction(title: NSLocalizedString("BUTTON_OK", comment: "确定"),

                                        style: .cancel, handler: {

                                            action in

                                            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)

    }

    @objc func dismissAlertB()

    {

        // Dismiss the alert from here

        if Utils.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")

            })

相关文章

网友评论

    本文标题:swift自定Alert,指定时间,消失继续执行block内容

    本文链接:https://www.haomeiwen.com/subject/vecrqqtx.html