开发者

MacRuby: Restrict NSTextField to Integers

I'm just picking up the basics of Cocoa and Interface Builder -- using IB version 3.2.6开发者_如何学Go. I'm looking to learn how to use MacRuby for native OS X apps.

I have an NSTextField that I would like to only accept integer values. I used NSNumberFormatter but it doesn't seem to have an option to only accept integers.

How would you do this? Can it be done in Interface Builder?


A naive but effective approach would be to subclass NSTextField and (this is the naive part) ovverride textDidChange to force the digited value to a number:

In other words, create a new ruby file in your Classes folder add this code to It and change the field class inside IB (your xib > filed you want to change > inspector > Class dropdown)

class OnlyNumbersTextField < NSTextField
    def textDidChange(notification)

        self.setIntValue(self.intValue)

    end
end

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜