简体   繁体   中英

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, ie 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?

请参阅核心数据编程指南的托管对象验证一章。

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.

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