user2924482
user2924482

Reputation: 9140

Cocoa swift: how can set NSTextField to only accept number

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.

Upvotes: 1

Views: 1060

Answers (1)

James Bucanek
James Bucanek

Reputation: 3439

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.

Upvotes: 3

Related Questions