Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 864e7d7

Browse files
committed
weakref for control events
1 parent 48e46f9 commit 864e7d7

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

widgets/src/iosMain/kotlin/dev/icerock/moko/widgets/core/factory/ButtonWithIconViewFactory.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ actual class ButtonWithIconViewFactory actual constructor(
100100

101101
button.setEventHandler(
102102
controlEvent = UIControlEventTouchUpInside,
103-
action = widget.onTap
103+
action = { widget.onTap }
104104
)
105105

106106
val contentAttribute: UISemanticContentAttribute

widgets/src/iosMain/kotlin/dev/icerock/moko/widgets/core/utils/UIControlExt.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ import platform.objc.OBJC_ASSOCIATION_RETAIN
2020
import platform.objc.objc_setAssociatedObject
2121
import kotlin.native.ref.WeakReference
2222

23-
fun UIControl.setEventHandler(controlEvent: UIControlEvents, action: () -> Unit) {
24-
val target = LambdaTarget(action)
23+
fun <V: UIControl> V.setEventHandler(controlEvent: UIControlEvents, action: (V) -> Unit) {
24+
val weakReference: WeakReference<V> = WeakReference(this)
25+
val target = LambdaTarget {
26+
val strongRef: V = weakReference.get() ?: return@LambdaTarget
27+
28+
action(strongRef)
29+
}
2530

2631
addTarget(
2732
target = target,

0 commit comments

Comments
 (0)