2014-09-18 3 views

ответ

1

В отличие от UIAlertView, SDCAlertView добавлен в виде иерархии вида. Это означает, что вы можете просто добавить распознаватель жестов к окну SDCAlertView, который вызывает [SDCAlertView dismissWithClickedButtonIndex:animated:].

+0

Как это сделать теперь с помощью SDCAlertController? – ChikabuZ

+0

Вы можете попробовать добавить его к виду контроллера, но это может быть не совсем то, что вы хотите –

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() 
     } 
    } 
}