Cocoa 是否在*编辑期间替换NSTextFieldCell*的内容?
如果用户在编辑NSTextFieldCell时输入了某些文本,我将尝试将NSTextFieldCell的内容加粗。 到目前为止,我得到了这个: 在awakeFromNib中:Cocoa 是否在*编辑期间替换NSTextFieldCell*的内容?,cocoa,nstableview,Cocoa,Nstableview,如果用户在编辑NSTextFieldCell时输入了某些文本,我将尝试将NSTextFieldCell的内容加粗。 到目前为止,我得到了这个: 在awakeFromNib中: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldCellDidChange:) name:NSControlTextDidChangeNotification object:theNSTableView]
[[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的编辑内容?要在进行更改后获取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) ;
}
谢谢你的信息,约翰。在编辑过程中,是否会在每次按键时调用此方法,或者,是否仅在编辑完成后调用此方法?在我的测试中,它是在每次按键后调用的。太棒了。非常感谢,约翰!