Hello, i’m trying to call addTarget on an UITextFi...
# ios
g
Hello, i’m trying to call addTarget on an UITextField but it crash at runtime. Kotlin code
Copy code
init {
        uiView.searchTextField.addTarget(this, platform.darwin.sel_registerName("searchTextChanged"), UIControlEventEditingChanged)
    }

    @ObjCAction
    private fun searchTextChanged() {
        // some code 
    }
iOS Crash
Copy code
2020-01-08 15:34:46.891004+0100 Production[69085:8719071] -[kobjcc0 searchTextChanged]: unrecognized selector sent to instance 0x6000036b1620
2020-01-08 15:34:46.897007+0100 Production[69085:8719071] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[kobjcc0 searchTextChanged]: unrecognized selector sent to instance 0x6000036b1620'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff23c7127e __exceptionPreprocess + 350
	1   libobjc.A.dylib                     0x00007fff513fbb20 objc_exception_throw + 48
	2   CoreFoundation                      0x00007fff23c91fd4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
	3   CoreFoundation                      0x00007fff23c75c4c ___forwarding___ + 1436
	4   CoreFoundation                      0x00007fff23c77f78 _CF_forwarding_prep_0 + 120
	5   UIKitCore                           0x00007fff48093fff -[UIApplication sendAction:to:from:forEvent:] + 83
	6   UIKitCore                           0x00007fff47a6c00e -[UIControl sendAction:to:forEvent:] + 223
	7   UIKitCore                           0x00007fff47a6c358 -[UIControl _sendActionsForEvents:withEvent:] + 398
	8   UIKitCore                           0x00007fff4839655f -[UITextField fieldEditorDidChange:] + 202
	9   UIKitCore                           0x00007fff4837b53a -[UIFieldEditor textInputDidChange:] + 61
	10  UIKitCore                           0x00007fff483a6d45 -[UITextInputController _sendDelegateChangeNotificationsForText:selection:] + 104
	11  UIKitCore                           0x00007fff483a9628 -[UITextInputController _insertText:fromKeyboard:] + 765
	12  UIKitCore                           0x00007fff483aa07f -[UITextInputController insertText:] + 336
	13  UIKitCore                           0x00007fff4837ec97 -[UIFieldEditor insertFilteredText:] + 838
	14  UIKitCore                           0x00007fff4839c5e3 -[UITextField insertFilteredText:] + 93
	15  UIKitCore                           0x00007fff47ea906b -[UIKeyboardImpl insertText:] + 147
	16  UIKitCore                           0x00007fff47ea3863 -[UIKeyboardImpl _performKeyboardOutput:shouldCheckDelegate:] + 1059
	17  UIKitCore                           0x00007fff47ea24bd __55-[UIKeyboardImpl handleKeyboardInput:executionContext:]_block_invoke_2 + 372
	18  UIKitCore                           0x00007fff47ed1ab1 -[UIKeyboardTaskEntry execute:] + 147
	19  UIKitCore                           0x00007fff47ed05c7 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 310
	20  Foundation                          0x00007fff25781c30 __NSThreadPerformPerform + 259
	21  CoreFoundation                      0x00007fff23bd4471 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
	22  CoreFoundation                      0x00007fff23bd439c __CFRunLoopDoSource0 + 76
	23  CoreFoundation                      0x00007fff23bd3b74 __CFRunLoopDoSources0 + 180
	24  CoreFoundation                      0x00007fff23bce87f __CFRunLoopRun + 1263
	25  CoreFoundation                      0x00007fff23bce066 CFRunLoopRunSpecific + 438
	26  GraphicsServices                    0x00007fff384c0bb0 GSEventRunModal + 65
	27  UIKitCore                           0x00007fff48092d4d UIApplicationMain + 1621
	28  Production                          0x000000010c1b1359 platform_UIKit_UIApplicationMain_wrapper64 + 89
	29  Production                          0x000000010c10a7cb kfun:main(kotlin.Array<kotlin.String>) + 1371
	30  Production                          0x000000010c10ab1c Konan_start + 156
	31  Production                          0x000000010c17249f Init_and_run_start + 111
	32  libdyld.dylib                       0x00007fff5227ec25 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
my code is inspired from KotlinFinder project ( https://github.com/JetBrains/KotlinFinder/blob/master/ios-app/src/iosMain/kotlin/Views/CollectWordView.kt )
a
Can you try NSSelectorFromString("searchTextChanged") instead of platform.darwin.sel_registerName? It's part of the platform.Foundation library.
g
I’m trying 🙂
same error sadly
The problem was the method “searchTextChanged” wasn’t in a NSObject subclass !
👍 2