Last active
February 17, 2021 03:47
-
-
Save Moximillian/5f6d60e2cd1222e557547a42558669ae to your computer and use it in GitHub Desktop.
Swift protocol and extensions for adding support to closures in UIKit classes. Extension support for UIButton, UIGestureRecognizer, UIBarButtonItem etc. Swift 3.0.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Free to copy, use and modify for all types of software projects | |
import UIKit | |
// ************** Protocol *************** | |
/// Closurable protocol | |
protocol Closurable: class {} | |
// restrict protocol to only classes => can refer to the class instance in the protocol extension | |
extension Closurable { | |
// Create container for closure, store it and return it | |
func getContainer(for closure: @escaping (Self) -> Void) -> ClosureContainer<Self> { | |
weak var weakSelf = self | |
let container = ClosureContainer(closure: closure, caller: weakSelf) | |
// store the container so that it can be called later, we do not need to explicitly retrieve it. | |
objc_setAssociatedObject(self, Unmanaged.passUnretained(self).toOpaque(), container as AnyObject!, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) | |
return container | |
} | |
} | |
/// Container class for closures, so that closure can be stored as an associated object | |
final class ClosureContainer<T: Closurable> { | |
var closure: (T) -> Void | |
var caller: T? | |
init(closure: @escaping (T) -> Void, caller: T?) { | |
self.closure = closure | |
self.caller = caller | |
} | |
// method for the target action, visible to UIKit classes via @objc | |
@objc func processHandler() { | |
if let caller = caller { | |
closure(caller) | |
} | |
} | |
// target action | |
var action: Selector { return #selector(processHandler) } | |
} | |
// ************** UIKit extensions *************** | |
/// extension for UIButton - actions with closure | |
extension UIButton: Closurable { | |
func addTarget(forControlEvents: UIControlEvents = .touchUpInside, closure: @escaping (UIButton) -> Void) { | |
let container = getContainer(for: closure) | |
addTarget(container, action: container.action, for: forControlEvents) | |
} | |
} | |
/// extension for UIPageControl - actions with closure | |
extension UIPageControl: Closurable { | |
func addTarget(forControlEvents: UIControlEvents = .valueChanged, closure: @escaping (UIPageControl) -> Void) { | |
let container = getContainer(for: closure) | |
addTarget(container, action: container.action, for: forControlEvents) | |
} | |
} | |
/// extension for UIGestureRecognizer - actions with closure | |
extension UIGestureRecognizer: Closurable { | |
convenience init(closure: @escaping (UIGestureRecognizer) -> Void) { | |
self.init() | |
let container = getContainer(for: closure) | |
addTarget(container, action: container.action) | |
} | |
} | |
/// extension for UIBarButtonItem - actions with closure | |
extension UIBarButtonItem: Closurable { | |
convenience init(image: UIImage?, style: UIBarButtonItemStyle = .plain, closure: @escaping (UIBarButtonItem) -> Void) { | |
self.init(image: image, style: style, target: nil, action: nil) | |
let container = getContainer(for: closure) | |
target = container | |
action = container.action | |
} | |
convenience init(title: String?, style: UIBarButtonItemStyle = .plain, closure: @escaping (UIBarButtonItem) -> Void) { | |
self.init(title: title, style: style, target: nil, action: nil) | |
let container = getContainer(for: closure) | |
target = container | |
action = container.action | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @radianttap, this extension is purely additive, it does not override any standard APIs. I have tested that IBAction as well as regular addTarget() work fine together with this extension, when added to the same object in the Storyboard.
... however, adding any kind of protocol conformance to UIButton or similar, renders it impossible to drag any actions for those elements inside Interface builder. TouchUpInside and similar are simply not available. The workaround is to comment out the conformance, drag the actions, and then un-comment the conformance. After that all works fine.