【问题标题】:How to dismiss SDCAlertView with tap gesture?如何用点击手势关闭 SDCAlertView?
【发布时间】:2014-09-18 09:46:32
【问题描述】:

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

【问题讨论】:

    标签: ios sdcalertview sdcalertcontroller


    【解决方案1】:

    UIAlertView 不同,SDCAlertView 作为视图添加到视图层次结构中。这意味着您可以简单地将轻击手势识别器添加到SDCAlertView 的超级视图,该视图调用[SDCAlertView dismissWithClickedButtonIndex:animated:]

    【讨论】:

    • 现在如何使用 SDCAlertController?
    • 你可以尝试将它添加到视图控制器的视图中,但这可能不是你想要的
    【解决方案2】:

    我找到了一种方法。与 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()
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多