Skip to content

Instantly share code, notes, and snippets.

@IanKeen
Last active June 4, 2022 23:18
Show Gist options
  • Save IanKeen/57db93aee14b845f4090b1fcc048e94b to your computer and use it in GitHub Desktop.
Save IanKeen/57db93aee14b845f4090b1fcc048e94b to your computer and use it in GitHub Desktop.
Easy closure based access to UIControl events (instead of target/action)
protocol ControlEventBindable: class { }
extension UIControl: ControlEventBindable { }
extension UIBarButtonItem: ControlEventBindable { }
private struct Keys {
static var EventHandlers = "_EventHandlers"
}
// MARK: - Implementation
extension ControlEventBindable where Self: UIControl {
private var eventHandlers: [ControlEventHandler<Self>] {
get { return (objc_getAssociatedObject(self, &Keys.EventHandlers) as? [ControlEventHandler<Self>]) ?? [] }
set { objc_setAssociatedObject(self, &Keys.EventHandlers, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
}
/// Listen for `UIControlEvents` executing the provided closure when triggered
func on(_ events: UIControlEvents, call closure: @escaping (Self) -> Void) {
let handler = ControlEventHandler<Self>(sender: self, events: events, closure: closure)
self.eventHandlers.append(handler)
}
}
extension ControlEventBindable where Self: UIBarButtonItem {
private var eventHandlers: [BarButtonItemEventHandler<Self>] {
get { return (objc_getAssociatedObject(self, &Keys.EventHandlers) as? [BarButtonItemEventHandler<Self>]) ?? [] }
set { objc_setAssociatedObject(self, &Keys.EventHandlers, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
}
init(barButtonSystemItem: UIBarButtonSystemItem, closure: @escaping (Self) -> Void) {
self.init(barButtonSystemItem: barButtonSystemItem, target: nil, action: nil)
self.eventHandlers.append(BarButtonItemEventHandler<Self>(sender: self, closure: closure))
}
init(title: String?, style: UIBarButtonItemStyle, closure: @escaping (Self) -> Void) {
self.init(title: title, style: style, target: nil, action: nil)
self.eventHandlers.append(BarButtonItemEventHandler<Self>(sender: self, closure: closure))
}
}
private final class ControlEventHandler<Sender: UIControl>: NSObject {
let closure: (Sender) -> Void
init(sender: Sender, events: UIControlEvents, closure: @escaping (Sender) -> Void) {
self.closure = closure
super.init()
sender.addTarget(self, action: #selector(self.action), for: events)
}
@objc private func action(sender: UIControl) {
guard let sender = sender as? Sender else { return }
self.closure(sender)
}
}
private final class BarButtonItemEventHandler<Sender: UIBarButtonItem>: NSObject {
private let closure: (Sender) -> Void
private let cleanup: () -> Void
init(sender: Sender, closure: @escaping (Sender) -> Void) {
self.closure = closure
self.cleanup = {
sender.target = nil
sender.action = nil
}
super.init()
sender.target = self
sender.action = #selector(self.action)
}
deinit { cleanup() }
@objc private func action(sender: UIBarButtonItem) {
guard let sender = sender as? Sender else { return }
self.closure(sender)
}
}
let button = UIButton()
button.on(.touchUpInside) { button in
//use button as a UIButton
}
let barButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel) { button in
//use barbutton
}
MIT License
Copyright (c) [year] [fullname]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@IanKeen
Copy link
Author

IanKeen commented Jan 12, 2018

updated to include UIBarButtonItems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment