我有一个具有23列NSTableView的应用程序。 NSTableView的内容绑定到IB中的ArrayController.arrangedObjects。此外,表中包含的每个NSTextFieldCell都具有绑定到Table Cell View objectValue.someKey的值。我的一个列具有可编辑的值,因此,我实现了controlTextDidEndEditing委托方法。另一个表列包含错误文本,并绑定到objectValue.errorText。
上面提到的ArrayController有一个绑定到NSMutableArray的内容数组,这是我的ViewController的一个属性。这个数组包含一个" Event"对象,在应用程序启动时定义和验证。
因此controlTextDidFinishEditing:方法有一个通知参数,在这种情况下,它是从中调用的NSTextField。我想在这种方法中做的是访问基础"事件"对象,包含在绑定到ArrayController的NSMutableArray中,并设置"事件"的错误文本属性。反对@""。
我认为这有一个非常简单的答案,但我正在努力尝试正确地说出我的Google查询,以便得出我正在寻找的答案。
答案 0 :(得分:2)
这个问题已经有了答案,但如果未来读者想知道绑定对象,他们可以检查文本域的绑定。
- (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:@""];
}
答案 1 :(得分:0)
这个答案来自于实施@ stevesliva的第一个建议。
我将controlTextDidEndEditing:方法定义如下。
-(void)controlTextDidEndEditing:(NSNotification *)notification {
NSTextField *textField = [notification object];
NSView* superView = [textField superview];
Event* modifiedEvent = ((NSTableCellView*)superView).objectValue;
[modifiedEvent setTextColor:[NSColor blackColor]];
[modifiedEvent setErrorMessage:@""];
return;
}
这里的关键是将superview的返回强制转换为NSTableCellView *对象,而不是通用的NSView *。这样做将允许您访问objectValue属性。