简体   繁体   中英

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.

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:

在此输入图像描述

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

在此输入图像描述

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.

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