When a user adds a new managed object, it shows up in a table, which scrolls down to the new entry, and the name of the new object (a default value) goes into editing mode.
I need to check if the name of the new object is unique in the datastore, so I can’t use a formatter for this. I think the perfect moment where I should validate this, is whenever the user tries to commit the entry’s name value, using textShouldEndEditing:.
I subclassed NSTableView and overrid following methods, just to be able to check in the log if they get called.
- (BOOL)textShouldEndEditing:(NSText *)textObject {
NSLog(@"textSHOULDendEditing fired in MyTableView");
return [super textShouldEndEditing:textObject];
}
- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor {
NSLog(@"control:textShouldEndEditing fired in MyTableView");
return YES;
}
- (void)textDidEndEditing:(NSNotification *)aNotification {
NSLog(@"textDIDEndEditing fired in MyTableView");
}
textDidEndEditing: gets called fine, but textShouldEndEditing: does not.
In the NSTableView Class Reference, under Text Delegate Methods, both methods textShouldEndEditing: and textDidEndEditing: are listed. Someone please explain why one gets called and the other doesn’t.
I think the NSTableView acts as the delegate for an NSTextField that gets instantiated as a black box delegate for the NSTextFieldCell. So what is referred to as delegate methods in the NSTableView Class Reference, actually implement the text manipulating methods for the NSTextField object.
I tried to declare the NSTextFieldCell as an outlet in my NSTableView. I also tried to declare several protocols in the NSTableView.
#import <AppKit/AppKit.h>
#import <Cocoa/Cocoa.h>
@interface MyTableView : NSTableView <NSTextDelegate, NSTextFieldDelegate, NSControlTextEditingDelegate, NSTableViewDelegate, NSTableViewDataSource> {
}
@end
Don’t laugh, I even tried to declare my table view as its own delegate 😛
I overrid
-(void)awakeFromInsert;in the (subclassed) managed object, to construct a unique default value for the name-property.Also, I ended up not overriding the
-(BOOL)textShouldEndEditing:method in the table view. Instead, I check if a newly entered name-property is unique in the (subclassed) managed object’s-(BOOL)validate<Key>:error:.Together, the above two strategies result in unique name-properties in all managed objects.
Maybe I could have forced the
NSTextFieldCellto go into editing mode, resulting in-(BOOL)textShouldEndEditing:to get called every time.Some remarks though:
It seems
-(BOOL)textShouldEndEditing:returns NO when the-(BOOL)validate<Key>:error:returns NO.Both
-(BOOL)textShouldEndEditing:and-(BOOL)validate<Key>:error:methods are called only when the user actually makes changes to the property.