SHARE
TWEET

Untitled

a guest Apr 19th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Parameter is the type of parameter passed in the selector
  2. public class ClosureSelector<Parameter> {
  3.    
  4.     public let selector : Selector
  5.     private let closure : ( Parameter ) -> ()
  6.    
  7.     init(withClosure closure : @escaping ( Parameter ) -> ()){
  8.         self.selector = #selector(ClosureSelector.target)
  9.         self.closure = closure
  10.     }
  11.    
  12.     // Unfortunately we need to cast to AnyObject here
  13.     @objc func target( param : AnyObject) {
  14.         closure(param as! Parameter)
  15.     }
  16. }
  17.  
  18. extension UIControl {
  19.    
  20.     func addTarget(forControlEvents controlEvents : UIControl.Event, withClosure closure : @escaping (UIControl) -> Void) {
  21.         let closureSelector = ClosureSelector<UIControl>(withClosure: closure)
  22.         objc_setAssociatedObject(self, &handle, closureSelector, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  23.         self.addTarget(closureSelector, action: closureSelector.selector, for: controlEvents)
  24.     }
  25.    
  26. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top