Implementing NSText delegate methods in PyObjc and Cocoa

In the project that I am creating, I would like to call a method when I paste text into a specific text field. I can't seem to get this to work, but here's what I tried

I applied a custom class (based on NSObject) as a delegate for my text field, and then gave it a method: textDidChange:

class textFieldDelegate(NSObject):
    def textDidChange_(self, notification):
        NSLog("textdidchange")

Then I created an object of this class in the interface builder and assigned it as a delegate from NSTextField. This, however, does not seem to do anything. However, when I create the sample code from http://www.programmish.com/?p=30 , everything works fine. How can I embed this delegate code so that it really works?

+3
source share
1 answer

The reason this does not work is because textDidChange_it is not a delegate method. This is a method on NSTextFieldthat sends a change notification. If you look in the docs for textDidChange, you will see that it mentions the actual delegate method name:

This method causes the recipient delegate to receive the controlTextDidChange: message. See the NSControl class specification for more information on the text delegate method.

Delegate method is actually called controlTextDidChange_, and declared in a superclass NSTextField, NSControl.

Change your delegation method to:

def controlTextDidChange_(self, notification):
    NSLog("textdidchange")

and it should work for you.

+3
source

Source: https://habr.com/ru/post/1709516/


All Articles