NSTableView and data validation?
In order to properly learn Objective-C and the Cocoa framework, I have started building a CoreData application, but I'm a bit at a loss on how to properly provide data validation when an item is being edited in an NSTableView.
My model has an entity called "Business" with two string properties: a name and an issued tax number. The second property must have a length of exactly 10 characters. This entity's objects are displayed and edited in an NSTableView, through an NSArrayController bound to the Business entity and the app's managedObjectContext.
What I would like to do is: - the user starts editing the 开发者_运维问答tax number in the appropriate cell (NSTextFieldCell) of the tableview. - when pressing Enter, the new value is validated, i.e. checks to see that the new value has a length of 10 characters. If validation fails, an alert sounds and the cell keeps being focused. - pressing Escape restores the old value and the cell loses focus.
What is the proper way to perform this validation?
See the Managed Object Validation chapter of the Core Data Programming Guide.
If you are using bindings, you can check the "Validates Immediately" in the binding for cell value. That will call the validation method for the managed object.
精彩评论