The signature for addTarget
(in Kotlin) is:
public open external expect fun addTarget(
target: Any?,
action: COpaquePointer? /* = CPointer<out CPointed>? */,
forControlEvents: UIControlEvents /* = ULong */
): Unit
I think I get how I'd hand in a C function pointer, but that doesn't seem to be the case here...?
Found the solution here: https://discuss.kotlinlang.org/t/how-to-call-a-selector-from-kotlin-for-ios/4591
The key is to use sel_registerName
to create the pointer and to annotate the target with @ObjCAction
:
import kotlinx.cinterop.ObjCAction
import platform.UIKit.*
import platform.objc.sel_registerName
class MyClass() {
val uiButton = UIButton.buttonWithType(UIButtonTypeSystem)
init {
uiButton.setTitle("Click me", UIControlStateNormal)
uiButton.addTarget(this, sel_registerName("clicked"), UIControlEventTouchUpInside)
}
@ObjCAction
fun clicked() {
// React to click here...
}
}