Swift:NSTextField numberStyle 不调用动作 [英] Swift: NSTextField numberStyle not calling action
问题描述
我有一个使用以下代码设置的 NSTextField:
I have a NSTextField that I have setup with the following code:
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
}
我还为在 NSTextFieldDelegate 中修改字段时设置了一个操作.
I have also set up an action set up for when the field is modified in the NSTextFieldDelegate.
@IBAction func saveCell(sender: AnyObject)
{
print("saveCell")
}
我的问题是 saveCell 操作仅在我输入与货币样式完全匹配的值时触发.即输入 45 英镑没问题,但当我输入普通的 45 时没有任何反应.
My problem is that the saveCell action is only triggered when I type in a value that exactly matches the currency style. i.e. typing £45 is OK but nothing happens when I enter a plain 45.
如果我删除 NSNumberFormatter,它将接受任何条目,但不会按照我想要的方式格式化 textField.
If I remove the NSNumberFormatter then it will accept any entry, but doesn't format the textField in the way that I want it.
如何接受没有货币符号的 45 值?这样做的最佳方法是什么?
How can I accept the 45 value without the currency symbol? What's the best way of doing this?
推荐答案
你可以子类化你的 NSTextField 如下:
You can subclass your NSTextField as follow:
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)
}
}
<小时>
编辑/更新:
如果你需要一个整数字段,你可以做如下:
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)
}
}
这篇关于Swift:NSTextField numberStyle 不调用动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!