Skip to content

Commit 994d931

Browse files
rebello95Reflejo
authored andcommitted
Add replaceEventHandler to UIControl (#26)
Adds a `replaceEventHandler` function to `UIControl` to act as a convenience function for removing event handlers and adding a new one.
1 parent 8705e70 commit 994d931

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

LambdaKit.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'LambdaKit'
3-
s.version = '0.2.0'
3+
s.version = '0.2.1'
44
s.license = 'MIT'
55
s.summary = 'Closures on most used UIKit methods'
66
s.homepage = 'https://github.com/Reflejo/LambdaKit'

Source/UIControl+LambdaKit.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ extension UIControl {
6262

6363
set {
6464
objc_setAssociatedObject(self, &associatedEventHandle, newValue,
65-
objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
65+
objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
6666
}
6767
}
6868

6969
/// Adds a closure for a particular event to an internal dispatch table.
7070
///
7171
/// - parameter controlEvents: A bitmask specifying the control events for which the action message is
7272
/// sent.
73-
/// - parameter handler: A block representing an action message, with an argument for the sender.
73+
/// - parameter handler: A closure representing an action message, with an argument for the sender.
7474
public func addEventHandler(forControlEvents controlEvents: UIControlEvents,
7575
handler: @escaping LKControlHandler)
7676
{
@@ -99,8 +99,20 @@ extension UIControl {
9999
self.events?[event] = nil
100100
for wrapper in wrappers {
101101
self.removeTarget(wrapper, action: #selector(ControlWrapper.invoke(_:)),
102-
for: UIControlEvents(rawValue: event))
102+
for: UIControlEvents(rawValue: event))
103103
}
104104
}
105105
}
106+
107+
/// Convenience function for setting a control's handler. Removes all other handlers for the provided
108+
/// events.
109+
///
110+
/// - parameter controlEvents: A bitmask specifying the control events that the handler will replace.
111+
/// - parameter handler: A closure representing an action message, with an argument for the sender.
112+
public func setEventHandler(forControlEvents controlEvents: UIControlEvents,
113+
handler: @escaping LKControlHandler)
114+
{
115+
self.removeEventHandlers(forControlEvents: controlEvents)
116+
self.addEventHandler(forControlEvents: controlEvents, handler: handler)
117+
}
106118
}

0 commit comments

Comments
 (0)