我在 controlTextDidEndEditing
中检查并删除 NSTextField
中的空格,但这些更改未反射(reflect)在 cocoa 绑定(bind)中(CoreData 条目未更新)。 NSTextField
已更新,但如果您单击条目外并返回到空格中,则会返回。 如何触发绑定(bind)对象更新数据存储?
我绑定(bind)了 NSTextField
的 .value,我什至尝试在更新后将 .objectValue 设置为 .stringValue,但它不起作用。
override func controlTextDidEndEditing(obj: NSNotification) {
Swift.print("editing is done now")
let textField:NSTextField = obj.object as! NSTextField
//if last character is a space remove it.
while textField.stringValue.characters.last == " "
{
Swift.print("last char is a space")
textField.stringValue.removeAtIndex(textField.stringValue.endIndex.predecessor())
}
//save to database now.
let dele:AppDelegate = NSApplication.sharedApplication().delegate as! AppDelegate
dele.saveAction(nil)
}
最佳答案
textField.stringValue
不是引用类型。您可以将该值分配回文本字段。
有更方便的修剪字符串的方法
override func controlTextDidEndEditing(obj: NSNotification) {
Swift.print("editing is done now")
let textField = obj.object as! NSTextField
let string = textField.stringValue
textField.stringValue = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
//save to database now.
let dele:AppDelegate = NSApplication.sharedApplication().delegate as! AppDelegate
dele.saveAction(nil)
}
关于swift - 验证后强制 cocoa 绑定(bind)更新(快速),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35685715/