Skip to content

Commit d3d91ef

Browse files
author
coderchan
committed
Controll Fix bug
1 parent a33b8ae commit d3d91ef

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

SwiftlyUI/Source/Core/UIControl+SwiftlyUI.swift

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,34 +47,42 @@ public extension UIControl {
4747
/// .onAction(target: self, action: { $0.doSomething() })
4848
@discardableResult
4949
func onAction<T: AnyObject>(target: T, for event: UIControl.Event = .touchUpInside, action: @escaping (T) -> Void) -> Self {
50-
if let oldWrapper = actionClosures[event.rawValue] {
50+
var actionClosure = actionClosures
51+
if let oldWrapper = actionClosure[event.rawValue] {
5152
removeTarget(oldWrapper, action: nil, for: event)
53+
actionClosure.removeValue(forKey: event.rawValue)
5254
}
5355

54-
if let oldHandler = actionClosures[event.rawValue] as? ActionSingleHandler<T> {
56+
if let oldHandler = actionClosure[event.rawValue] as? ActionSingleHandler<T> {
5557
removeTarget(oldHandler, action: nil, for: event)
58+
actionClosure.removeValue(forKey: event.rawValue)
5659
}
5760

5861
let handler = ActionSingleHandler(target: target, action: action)
5962
addTarget(handler, action: #selector(ActionSingleHandler<T>.invoke), for: event)
60-
actionClosures[event.rawValue] = handler
63+
actionClosure[event.rawValue] = handler
64+
actionClosures = actionClosure
6165
return self
6266
}
6367

6468
/// .onAction(target: self, action: { (vc: ViewController, btn: UIButton) in vc.doSomething(with: btn) })
6569
@discardableResult
6670
func onAction<T: AnyObject, ControlType: UIControl>(target: T, for event: UIControl.Event = .touchUpInside, action: @escaping (T, ControlType) -> Void) -> Self {
67-
if let oldWrapper = actionClosures[event.rawValue] {
71+
var actionClosure = actionClosures
72+
if let oldWrapper = actionClosure[event.rawValue] {
6873
removeTarget(oldWrapper, action: nil, for: event)
74+
actionClosure.removeValue(forKey: event.rawValue)
6975
}
7076

71-
if let oldHandler = actionClosures[event.rawValue] as? ActionHandler<T, ControlType> {
77+
if let oldHandler = actionClosure[event.rawValue] as? ActionHandler<T, ControlType> {
7278
removeTarget(oldHandler, action: nil, for: event)
79+
actionClosure.removeValue(forKey: event.rawValue)
7380
}
7481

7582
let handler = ActionHandler(target: target, action: action)
7683
addTarget(handler, action: #selector(ActionHandler<T, ControlType>.invoke(_:)), for: event)
77-
actionClosures[event.rawValue] = handler
84+
actionClosure[event.rawValue] = handler
85+
actionClosures = actionClosure
7886
return self
7987
}
8088

@@ -88,14 +96,16 @@ public extension UIControl {
8896
/// button.onAction { [weak self] (btn: UIButton) in self?.doSomething() }
8997
@discardableResult
9098
func onAction<T: UIControl>(for event: UIControl.Event = .touchUpInside, action: @escaping (T) -> Void) -> Self {
99+
var actionClosure = actionClosures
91100
if let oldWrapper = actionClosures[event.rawValue] {
92101
removeTarget(oldWrapper, action: nil, for: event)
102+
actionClosure.removeValue(forKey: event.rawValue)
93103
}
94104

95105
let wrapper = ClosureWrapper<T>(button: self as! T, closure: action)
96106
addTarget(wrapper, action: #selector(ClosureWrapper.invoke), for: event)
97107

98-
var closures = actionClosures
108+
var closures = actionClosure
99109
closures[event.rawValue] = wrapper
100110
actionClosures = closures
101111
return self
@@ -157,23 +167,16 @@ private class ClosureWrapper<T: UIControl>: NSObject {
157167
}
158168

159169
private extension UIControl {
160-
struct AssociatedKeys {
170+
struct UIControlAssociatedKeys {
161171
nonisolated(unsafe) static var actionClosuresKey: Void?
162172
}
163173
private var actionClosures: [UInt: Any] {
164174
get {
165-
return objc_getAssociatedObject(
166-
self,
167-
withUnsafePointer(to: &AssociatedKeys.actionClosuresKey) { UnsafeRawPointer($0) }
168-
) as? [UInt: ClosureWrapper] ?? [:]
175+
let aaa = objc_getAssociatedObject(self, &UIControlAssociatedKeys.actionClosuresKey)
176+
return objc_getAssociatedObject(self, &UIControlAssociatedKeys.actionClosuresKey) as? [UInt: Any] ?? [:]
169177
}
170178
set {
171-
objc_setAssociatedObject(
172-
self,
173-
withUnsafePointer(to: &AssociatedKeys.actionClosuresKey) { UnsafeRawPointer($0) },
174-
newValue,
175-
.OBJC_ASSOCIATION_RETAIN_NONATOMIC
176-
)
179+
objc_setAssociatedObject(self, &UIControlAssociatedKeys.actionClosuresKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
177180
}
178181
}
179182

0 commit comments

Comments
 (0)