Skip to main content
100%

Swift protocol and extensions for adding support to closures in UIKit classes. Extension support for UIButton, UIGestureRecognizer, UIBarButtonItem etc. Swift 3.0.

✓ Published0🌍 Public
MMoximillian
Last edited Feb 4, 2017
Created on Mar 29, 2016

This example demonstrates a Swift protocol that adds closure-based event handling to UIKit classes, replacing traditional target-action patterns. Code extensions for UIButton, UIPageControl, UIGestureRecognizer, and UIBarButtonItem use the Closurable protocol to wrap callbacks in a ClosureContainer object, stored via objc_setAssociatedObject. The container exposes an @objc selector that invokes the stored closure, allowing UIKit to call the handler dynamically. The code pairs each UI component’s standard addTarget or initializer with custom convenience methods, keeping the call site concise. No external libraries are required; the implementation relies solely on Swift 3.0 and the Objective-C runtime.

AI-generated description

Similar vizzes