Skip to content

Commit

Permalink
Add replaceEventHandler to UIControl (#26)
Browse files Browse the repository at this point in the history
Adds a `replaceEventHandler` function to `UIControl` to act as a
convenience function for removing event handlers and adding a new one.
  • Loading branch information
rebello95 authored and Reflejo committed Jul 20, 2017
1 parent 8705e70 commit 994d931
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion LambdaKit.podspec
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'LambdaKit'
s.version = '0.2.0'
s.version = '0.2.1'
s.license = 'MIT'
s.summary = 'Closures on most used UIKit methods'
s.homepage = 'https://github.com/Reflejo/LambdaKit'
Expand Down
18 changes: 15 additions & 3 deletions Source/UIControl+LambdaKit.swift
Expand Up @@ -62,15 +62,15 @@ extension UIControl {

set {
objc_setAssociatedObject(self, &associatedEventHandle, newValue,
objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}

/// Adds a closure for a particular event to an internal dispatch table.
///
/// - parameter controlEvents: A bitmask specifying the control events for which the action message is
/// sent.
/// - parameter handler: A block representing an action message, with an argument for the sender.
/// - parameter handler: A closure representing an action message, with an argument for the sender.
public func addEventHandler(forControlEvents controlEvents: UIControlEvents,
handler: @escaping LKControlHandler)
{
Expand Down Expand Up @@ -99,8 +99,20 @@ extension UIControl {
self.events?[event] = nil
for wrapper in wrappers {
self.removeTarget(wrapper, action: #selector(ControlWrapper.invoke(_:)),
for: UIControlEvents(rawValue: event))
for: UIControlEvents(rawValue: event))
}
}
}

/// Convenience function for setting a control's handler. Removes all other handlers for the provided
/// events.
///
/// - parameter controlEvents: A bitmask specifying the control events that the handler will replace.
/// - parameter handler: A closure representing an action message, with an argument for the sender.
public func setEventHandler(forControlEvents controlEvents: UIControlEvents,
handler: @escaping LKControlHandler)
{
self.removeEventHandlers(forControlEvents: controlEvents)
self.addEventHandler(forControlEvents: controlEvents, handler: handler)
}
}

0 comments on commit 994d931

Please sign in to comment.