2010-03-13 27 views
1

我有一個窗口內的NSTextField,我創造了一個非常簡單的MacRuby委託:綁定上的NSTextField的textDidChange事件到MacRuby的委託

class ServerInputDelegate 
    attr_accessor :parent 

    def textDidChange(notification) 
     NSLog notification.inspect 
     parent.filter 
    end 
end 

我試圖設置控件的委託:

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

我已經嘗試設置窗口和其他所有我能想到的代表。我也嘗試將其設置爲其他代表(例如應用程序),並正確地觸發applicationDidFinishLaunching等事件。

是否有任何技巧我缺少,以便每次此NSTextField的內容更改時觸發此事件?

回答

3

NSTextField的子類,然後在IB中將您希望子類的文本字段的Class設置爲「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} 
} 
3

textDidChange:,或許令人混淆,是一種NSTextDelegate method,這意味着它僅適用於NSText(因此NSTextView)對象。對於NSTextField,您應該只使用NSControl delegate方法controlTextDidChange:無需子類。

+0

投票你,不是因爲你回答了這個問題,而是因爲你解決了我遇到的另一個問題 - 謝謝! – Reuben 2012-03-12 00:59:57