Reputation: 163
I have an application that has a 23 column NSTableView. The NSTableView's content is bound to ArrayController.arrangedObjects in IB. In addition, each NSTextFieldCell contained within the table has value bound to Table Cell View objectValue.someKey. One of my columns has an editable value, so, I implemented the controlTextDidEndEditing delegate method. Another table column contains error text, and it is bound to objectValue.errorText.
The ArrayController mentioned above has a Content Array that is bound to an NSMutableArray, which is a property of my ViewController. This array contains a collection of "Event" objects, which are defined and validated when the application launches.
So the controlTextDidFinishEditing: method has a notification parameter, which in this case is the NSTextField that it was called from. What I would like to do in this method is access the underlying "Event" object, contained in the NSMutableArray that is bound to the ArrayController, and set the error text property of the "Event" object to @"".
I would imagine that this has a very simple answer, but I'm struggling trying to phrase my Google query correctly to yield the answers I'm looking for.
Upvotes: 0
Views: 171
Reputation: 15598
This question already has an answer but if future readers want to know the bound object, they can inspect the binding of the textfield.
- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
NSTextField *textField = [aNotification object];
NSDictionary* dictionary = [textField infoForBinding:NSValueBinding];
NSTableCellView *tableCellView = [dictionary objectForKey:NSObservedObjectKey];
NSString *keyPath = [dictionary objectForKey:NSObservedKeyPathKey];
Event* modifiedEvent = tableCellView.objectValue;
[modifiedEvent setTextColor:[NSColor blackColor]];
[modifiedEvent setErrorMessage:@""];
}
Upvotes: 2
Reputation: 163
This answer comes from implementing @stevesliva's first suggestion.
I defined the controlTextDidEndEditing: method as follows.
-(void)controlTextDidEndEditing:(NSNotification *)notification {
NSTextField *textField = [notification object];
NSView* superView = [textField superview];
Event* modifiedEvent = ((NSTableCellView*)superView).objectValue;
[modifiedEvent setTextColor:[NSColor blackColor]];
[modifiedEvent setErrorMessage:@""];
return;
}
The key here is to cast the return of superview as a NSTableCellView* object, instead of a generic NSView*. Doing so will allow you access to the objectValue property.
Upvotes: 0