在*编辑期间替换NSTextFieldCell *的内容?

时间:2013-07-16 17:54:38

标签: cocoa nstableview

如果在编辑NSTextFieldCell时用户输入了某些文本,我正在尝试用粗体显示NSTextFieldCell的内容。

到目前为止,我已经得到了这个:

在awakeFromNib中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldCellDidChange:)  name:NSControlTextDidChangeNotification object:theNSTableView];

方法:

- (void) textFieldCellDidChange: (id) sender
{
    //successfully captures contents of the NSTextFieldCell prior to the 
    //start of editing
    NSString * textOfMyNSTextFieldCell = [myNSTextFieldCell stringValue];

    //attempts to capture the current edited contents of the NSTextFieldCell while
    //editing is still in progress:
    //but DOES NOT YET WORK:
    NSText *fieldEditor = [myTableView currentEditor];
    textOfMyNSTextFieldCell = [fieldEditor string];
}

在编辑过程中是否可以捕获NSTextFieldCell的编辑内容?

1 个答案:

答案 0 :(得分:2)

要在更改后获取NSTextFieldCell的stringValue,请将NSTextFieldCell子类化并实现:

- (void) textDidChange:(NSNotification*)notification {
    NSLog(@"NSTextFieldCell noticed that text did change to: %@", self.stringValue) ;
    NSLog(@"Was notified by: \n%@", notification.object) ;
    NSLog(@"which is its controlView's currentEditor: \n%@", ((NSTextField*)self.controlView).currentEditor) ;
}