使用 NSTextFieldDelegate 时 – control:textView:doCommandBySelector:我们如何 allow/make 正常执行例如退格?
When using NSTextFieldDelegate's – control:textView:doCommandBySelector: how do we allow/make normal performance of e.g. backspace?
在另一个 SO 线程 (Execute an Action when the Enter-Key is pressed in a NSTextField?) 中,我看到了这个建议的代码,用于执行在 NSTextField 中按 Enter 的操作:
- (void)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
{
NSLog(@"Selector method is (%@)", NSStringFromSelector( commandSelector ) );
if (commandSelector == @selector(insertNewline:)) {
//Do something against ENTER key
} else if (commandSelector == @selector(deleteForward:)) {
//Do something against DELETE key
} else if (commandSelector == @selector(deleteBackward:)) {
//Do something against BACKSPACE key
} else if (commandSelector == @selector(insertTab:)) {
//Do something against TAB key
}
}
但是当我使用它时,退格键和制表符不再正常工作。如何解决这个问题?
我试图对原始 post 发表评论,但没有足够的声誉来 post 评论...TIA
control:doCommandBySelector:
应该 return 一个 bool
。 Return true
如果您已经处理了事件并且您不希望系统再做任何事情,return false
如果您仍然希望系统执行其默认实现对于给定的 selector
。所以当 selector
是退格键或制表符选择器时,你需要 return false
。
在另一个 SO 线程 (Execute an Action when the Enter-Key is pressed in a NSTextField?) 中,我看到了这个建议的代码,用于执行在 NSTextField 中按 Enter 的操作:
- (void)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
{
NSLog(@"Selector method is (%@)", NSStringFromSelector( commandSelector ) );
if (commandSelector == @selector(insertNewline:)) {
//Do something against ENTER key
} else if (commandSelector == @selector(deleteForward:)) {
//Do something against DELETE key
} else if (commandSelector == @selector(deleteBackward:)) {
//Do something against BACKSPACE key
} else if (commandSelector == @selector(insertTab:)) {
//Do something against TAB key
}
}
但是当我使用它时,退格键和制表符不再正常工作。如何解决这个问题?
我试图对原始 post 发表评论,但没有足够的声誉来 post 评论...TIA
control:doCommandBySelector:
应该 return 一个 bool
。 Return true
如果您已经处理了事件并且您不希望系统再做任何事情,return false
如果您仍然希望系统执行其默认实现对于给定的 selector
。所以当 selector
是退格键或制表符选择器时,你需要 return false
。