Skip to content

Commit 11f0dde

Browse files
committed
Added button handler compatibility pre-iOS 14
1 parent 58ac047 commit 11f0dde

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

Example/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// UIControl.swift
3+
// FeatureFlags
4+
//
5+
// Created by Ross Butler on 22/03/2023.
6+
//
7+
8+
import Foundation
9+
import ObjectiveC
10+
11+
public extension NSObject {
12+
func associatedObject<T>(key: UnsafeRawPointer, makeDefault: () -> T) -> T {
13+
if let result = objc_getAssociatedObject(self, key) as? T {
14+
return result
15+
}
16+
let result = makeDefault()
17+
objc_setAssociatedObject(self, key, result, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
18+
return result
19+
}
20+
}
21+
22+
public func configure<T>(_ value: T, using closure: (inout T) throws -> Void) rethrows -> T {
23+
var value = value
24+
try closure(&value)
25+
return value
26+
}
27+
28+
public final class TouchActionHandler: NSObject {
29+
public var action: () -> Void = {}
30+
@objc func didTouch() {
31+
action()
32+
}
33+
}
34+
35+
private var touchUpInsideHandlerKey: Int?
36+
37+
public extension UIControl {
38+
var touchUpInside: TouchActionHandler {
39+
associatedObject(key: &touchUpInsideHandlerKey) {
40+
configure(TouchActionHandler()) {
41+
self.addTarget($0, action: #selector(TouchActionHandler.didTouch), for: .touchUpInside)
42+
}
43+
}
44+
}
45+
}

FeatureFlags/Classes/UI/Feature Flags/View Controller/FeatureFlagsViewController.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@
99
import Foundation
1010
import UIKit
1111

12-
extension UIControl {
13-
func addAction(for controlEvents: UIControl.Event = .touchUpInside, _ closure: @escaping()->()) {
14-
if #available(iOSApplicationExtension 14.0, *) {
15-
addAction(UIAction { (action: UIAction) in closure() }, for: controlEvents)
16-
}
17-
}
18-
}
19-
2012
class FeatureFlagsViewController: UITableViewController {
2113

2214
// MARK: Type definitions
@@ -57,7 +49,7 @@ class FeatureFlagsViewController: UITableViewController {
5749
}
5850

5951
private func bindCell(_ cell: FeatureFlagTableViewCell, feature: Feature) -> FeatureFlagTableViewCell {
60-
cell.featureDetailButton.addAction {
52+
cell.featureDetailButton.touchUpInside.action = {
6153
let viewController = FeatureDetailsViewController(style: .grouped)
6254
viewController.feature = feature
6355
self.navigationController?.pushViewController(viewController, animated: true)

0 commit comments

Comments
 (0)