gpt4 book ai didi

python - 在 PyObjc 和 Cocoa 中实现 NSText 委托(delegate)方法

转载 作者:行者123 更新时间:2023-11-28 16:55:07 24 4
gpt4 key购买 nike

在我正在构建的项目中,我希望在将一些文本粘贴到特定文本字段时调用一个方法。我似乎无法让它发挥作用,但这是我尝试过的方法

我实现了一个自定义类(基于 NSObject)作为我的文本字段的委托(delegate),然后给它方法:textDidChange:

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

然后我在界面生成器中实例化了此类的一个对象,并将其设置为 NSTextField 的委托(delegate)。然而,这似乎没有做任何事情。但是,当我从 http://www.programmish.com/?p=30 构建示例代码时,一切似乎都运行良好。我如何实现此委托(delegate)代码以使其真正起作用?

最佳答案

这对您不起作用的原因是 textDidChange_ 不是委托(delegate)方法。这是 NSTextField 上的一种方法,用于发布更改通知。如果您查看过 textDidChange 的文档,您会发现它提到了委托(delegate)方法的实际名称:

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

委托(delegate)方法实际上称为 controlTextDidChange_ 并在 NSTextField 父类(super class) NSControl 上声明。

将您的委托(delegate)方法更改为:

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

它应该适合你。

关于python - 在 PyObjc 和 Cocoa 中实现 NSText 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/934628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com