如何用点击手势解雇SDCAlertView?

时间:2014-09-18 09:46:33

标签: ios sdcalertview sdcalertcontroller

要显示警报我使用SDCAlertView(UIAlertView的克隆)。我想通过点击屏幕来解除警报,如UIActionSheet。

2 个答案:

答案 0 :(得分:1)

UIAlertView不同,SDCAlertView被添加为视图层次结构的视图。这意味着您只需向SDCAlertView的超级视图添加点按手势识别器即可调出[SDCAlertView dismissWithClickedButtonIndex:animated:]

答案 1 :(得分:0)

我找到了一种方法来做到这一点。与iOS 7+兼容,但在8 +上可以点击。

class SmartAlert
{
    static var backroundWindow: UIWindow!
    static var alertController: SDCAlertController!

    class func showAlertWithTitle(title: String!, message: String!, actionTitle: String)
    {
        if (iOS8)
        {
            self.backroundWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
            self.backroundWindow.backgroundColor = UIColor.clearColor()
            self.backroundWindow.rootViewController = EmptyViewController()
            self.backroundWindow.windowLevel = UIWindowLevelAlert
            self.backroundWindow.makeKeyAndVisible()
        }

        self.alertController = SDCAlertController(title: title, message: message, preferredStyle: .Alert)
        self.alertController.addAction(SDCAlertAction(title: actionTitle, style: .Default, handler:
        {
            (_) -> Void in
            self.backroundWindow = nil
        }))

        self.alertController.presentWithCompletion(nil)
    }

    class func dismissAlert()
    {
        self.alertController.dismissWithCompletion
        {
            self.backroundWindow = nil
        }
    }
}

class EmptyViewController: UIViewController
{
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
    {
        SmartAlert.dismissAlert()
    }

    override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent)
    {
        if motion == UIEventSubtype.MotionShake
        {
            SmartAlert.dismissAlert()
        }
    }
}