如何确定NSSearchField/NSTextField是否具有输入焦点? [英] How to determine whether an NSSearchField/NSTextField has input focus?
问题描述
如何确定NSSearchField/NSTextField是否具有输入焦点?
How can I determine whether an NSSearchField/NSTextField has input focus?
推荐答案
上一个答案是错误的,因为NSTextField
/NSSearchField
本身不会成为第一响应者并处理已编辑的文本.相反,他们使用窗口的字段编辑器,这是一个NSTextView
,在窗口的所有字段之间共享(因为一次只能有一个焦点).
The previous answer is wrong, because NSTextField
/ NSSearchField
do not themselves become the first responder and handle edited text. Instead, they use the window's field editor, which is an NSTextView
that is shared between all fields on the window (since only one of them can have focus at a time).
您需要查看第一响应者是否为NSText
,如果是,则为搜索字段/文本字段是否为其委托.
You need to see if the first responder is an NSText
, and if so, if the search field / text field is its delegate.
NSResponder *firstResponder = [[NSApp keyWindow] firstResponder];
if ([firstResponder isKindOfClass:[NSText class]] && [(id)firstResponder delegate] == mySearchField) {
NSLog(@"Yup.");
}
这篇关于如何确定NSSearchField/NSTextField是否具有输入焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!