1

Any of you knows how can set NSTextField to only accept numbers(int)?

Let me explain. I got my NSTextField:

 @IBOutlet weak var txtBoxField: NSTextField!

But I can type any string and what I want is just to be able to type numbers.

I'll really appreciate your help.

user2924482
  • 8,380
  • 23
  • 89
  • 173

1 Answers1

3

In IB, get an NSNumberFormatter object and drop it onto your NSTextField. This attaches a number formatter object to that text field. In the object browser, it should look like this:

enter image description here

Now select the Number Formatter object and configure the kind, style, and range of numbers that it should allow:

enter image description here

Choose a style of None if you just want whole integers.

If you must do this programmatically, see NSNumberFormatter to create and configure the formatter object then use that to set the text field's formatter property.

James Bucanek
  • 3,299
  • 3
  • 14
  • 30