[英]Binding the textDidChange event on a NSTextField to a MacRuby delegate
我在窗口中有一个NSTextField,并且创建了一个非常简单的MacRuby委托:
class ServerInputDelegate
attr_accessor :parent
def textDidChange(notification)
NSLog notification.inspect
parent.filter
end
end
我尝试设置控件的委托:
我尝试设置此代理可以想到的Window和所有其他对象。 我还尝试将其设置为其他委托(例如,应用程序),并正确触发了诸如applicationDidFinishLaunching之类的事件。
我是否有任何技巧可以使每次此NSTextField的内容更改时触发此事件?
子类NSTextField,然后在IB中将希望子类化的文本字段的类设置为“ ServerInputDelegate”。 开始输入后,它应该会自动为您完成。
class ServerInputDelegate < NSTextField
def textDidChange(notification)
NSLog notification.description
puts self.stringValue
end
end
结果
2010-04-30 14:37:24.810 TextFieldTextChanged[69109:a0f] NSConcreteNotification 0x200350b00 {name = NSTextDidChangeNotification; object = <NSTextView: 0x2003b95e0>
Frame = {{2.00, 3.00}, {436.00, 17.00}}, Bounds = {{0.00, 0.00}, {436.00, 17.00}}
Horizontally resizable: YES, Vertically resizable: YES
MinSize = {436.00, 17.00}, MaxSize = {40000.00, 40000.00}
}
可能令人困惑的textDidChange:
是一个NSTextDelegate
方法 ,这意味着它仅适用于NSText
(因此适用于NSTextView
)对象。 对于NSTextField
,您应该只使用NSControl delegate
方法controlTextDidChange:
无需子类化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.