简体   繁体   中英

Binding the textDidChange event on a NSTextField to a MacRuby delegate

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:

alt text http://grab.by/31Kr

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?

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}
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM