Binding the textDidChange event on a NSTextField to a MacRuby delegate
-
19-09-2019 - |
Question
I have a NSTextField within a Window and I created a very simple MacRuby delegate:
class ServerInputDelegate
attr_accessor :parent
def textDidChange(notification)
NSLog notification.inspect
parent.filter
end
end
And I have tried setting the control's delegate:
I have tried setting the Window and every other object I could think of to this delegate. I have also tried setting it to other delegates (application for instance) and events like applicationDidFinishLaunching are being properly triggered.
Is there any trick I am missing in order for this event to be triggered every time the contents of this NSTextField changes?
Solution
Subclass NSTextField and then in IB set the Class of the text field you wish to subclass to "ServerInputDelegate." Once you start typing it should autocomplete for you.
class ServerInputDelegate < NSTextField
def textDidChange(notification)
NSLog notification.description
puts self.stringValue
end
end
result
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}
}
OTHER TIPS
textDidChange:
, perhaps confusingly, is an NSTextDelegate
method, which means it only works for NSText
(and therefore NSTextView
) objects. For an NSTextField
, you should just use the NSControl delegate
method controlTextDidChange:
No need to subclass.