[英]Swift: NSTextField numberStyle not calling action
I have a NSTextField that I have setup with the following code: 我有一个使用以下代码设置的NSTextField:
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
let cell : NSTableCellView? = tableView.makeViewWithIdentifier("AutomaticTableColumnIdentifier.0", owner: self) as! NSTableCellView?
cell!.textField!.floatValue = 4.55
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .CurrencyStyle
numberFormatter.maximumFractionDigits = 0
cell!.textField! = numberFormatter
return cell
}
I have also set up an action set up for when the field is modified in the NSTextFieldDelegate. 我还为在NSTextFieldDelegate中修改字段时设置了动作。
@IBAction func saveCell(sender: AnyObject)
{
print("saveCell")
}
My problem is that the saveCell action is only triggered when I type in a value that exactly matches the currency style. 我的问题是,仅当我键入与货币样式完全匹配的值时,才触发saveCell操作。 ie typing £45 is OK but nothing happens when I enter a plain 45.
例如,输入45英镑是可以的,但是当我输入纯45时什么也没发生。
If I remove the NSNumberFormatter then it will accept any entry, but doesn't format the textField in the way that I want it. 如果删除NSNumberFormatter,它将接受任何条目,但不会按照我想要的方式设置textField的格式。
How can I accept the 45 value without the currency symbol? 我如何接受不带货币符号的45值? What's the best way of doing this?
最好的方法是什么?
You can subclass your NSTextField as follow: 您可以如下子类化您的NSTextField:
import Cocoa
class CurrencyField: NSTextField {
var percent: Double {
return Double(Int(numbers) ?? 0) / 100
}
var currency: String {
return Number.currency.string(from: percent.number) ?? ""
}
var numbers: String {
return stringValue.components(separatedBy: CharacterSet(charactersIn: "0123456789").inverted).joined()
}
override func awakeFromNib() {
super.awakeFromNib()
alignment = .right
stringValue = currency
}
override func textDidChange(_ notification: Notification) {
Swift.print("--> textDidChange")
stringValue = currency
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
// Drawing code here.
}
}
extension NumberFormatter {
convenience init(numberStyle: NumberFormatter.Style) {
self.init()
self.numberStyle = numberStyle
}
}
struct Number {
static let currency = NumberFormatter(numberStyle: .currency)
}
extension Double {
var number: NSNumber {
return NSNumber(value: self)
}
}
Edit/Update: 编辑/更新:
If you need an Integer Field you can do as Follow: 如果需要整数字段,可以执行以下操作:
import Cocoa
class IntegerField: NSTextField {
var value: Int { return Int(numbers) ?? 0 }
var currency: String { return Number.currency.string(from: value.number) ?? "" }
var numbers: String { return stringValue.components(separatedBy: CharacterSet(charactersIn: "0123456789").inverted).joined() }
override func awakeFromNib() {
super.awakeFromNib()
alignment = .right
stringValue = currency
}
override func textDidChange(_ notification: Notification) {
Swift.print("--> textDidChange")
stringValue = currency
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
// Drawing code here.
}
}
extension NumberFormatter {
convenience init(numberStyle: NumberFormatter.Style, maximumFractionDigits: Int = 0) {
self.init()
self.numberStyle = numberStyle
self.maximumFractionDigits = maximumFractionDigits
}
}
struct Number {
static let currency = NumberFormatter(numberStyle: .currency)
}
extension Int {
var number: NSNumber {
return NSNumber(value: self)
}
}
Try: 尝试:
numberFormatter.isLenient = true
That should allow the NSTextField to work with or without the currency symbol. 那应该允许NSTextField使用或不使用货币符号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.